Wednesday, 11 January 2017

Coach View ,List Event Handlers


Change event handling for a coach view bound to a list of complex business object is different to event handling for a coach view bound to a simple type.The change handler scenarios that needs to be considered for the list of complex BO is as follows:

1. Handle changes when the list itself is updated?adding an item and removing an item to the list.
2. Handle changes when any element in the item of the list is updated ? (consider bindALL)
3. Handle changes when only one element of the item changes ?(Consider bind)

(Slightly modified example Referring : IBM BPM Coach Framework in Your Organization , page 80).


Sequence of execution and executable code:

1.Coach View Load event handler :

this.addHandles();

2. Inline Java Script:
//JS Array Objects holding the event handlers of the list of complex BO
this.connectHandles = [];

//JS Array Object holding row total
var rows = [] ;
//initialize
var finalTotal = this.context.options.finalTotal;
var totals;

//Add handles for all relevant actions through this function.:
this.addHandles = function () {
    this.bindAllHandle =this.context.binding.get("value").bindAll(this.bindAllChangeHandler, this);
    totals = this.context.binding.get("value");
    for (var x = 0; x < totals.length(); x++) {
    //Flavor 1:
    //this.setBindingHandler( { item: totals.get(x) } );
    //Flavor 2:
    this.setBindingHandler( totals.get(x),x );
    }
}
//setBindingHandler()
//flavor1
//this.setBindingHandler = function(config) {
//flavor2
this.setBindingHandler = function(item,x) {
    //Flavor 1
    //var item = config.item;
    //push(handler);
    this.connectHandles.push(
    //bindAll(changeHandler,scope)
        item.bindAll( function ( /*data1 */) {                                 
            var costs = this.get("costs") ? this.get("costs") : 0;
            var people = this.get("people") ? this.get("people") : 0;
            var ttl = people * costs;
            item.set("total",ttl);
            rows[x] = ttl;
            var temp = 0.0;
            for(var i=0;i<rows.length;i++){
                temp = temp+rows[i];
            }
        finalTotal.set("value", temp);  
        }, item)
    );
}
//If a new item is added to the totals list, refresh the bindings:
this.bindAllChangeHandler = function(event) {
   //update rows with the totals
    rows = [];
    var tempAllChangeHandler = 0.0;
    for(var i=0;i<totals.length();i++){
       rows[i] = totals.items[i].total;
       tempAllChangeHandler = tempAllChangeHandler+rows[i];
    }   
    finalTotal.set("value", tempAllChangeHandler);
    if ( event.newVal !== null &&
    event.newVal !== undefined &&
    event.newVal !== event.oldVal ) {
        this.refreshItemBindings();
    }
}
//Refresh all handles by removing them and then adding them again:
this.refreshItemBindings = function () {
    this.removeHandles();
    this.addHandles();
}
//You can remove all active handles through the remove handles function:
this.removeHandles = function () {
    if (this.bindAllHandle) { this.bindAllHandle.remove(); }
    array.forEach(this.connectHandles, function(handle) {
    if ( handle ) { handle.remove(); }
    });
    this.connectHandles = [];
   
}








Reference : 1.http://www.ibm.com/support/knowledgecenter/SSFTBX_8.5.0/com.ibm.wbpm.wle.editor.doc/develop/topics/rbindingdata.html
2.IBM BPM Coach Framework in Your Organization book , page 80