jQuery.Class.static.callback  function     

Returns a callback function for a function on this Class. The callback function ensures that 'this' is set appropriately.

$.Class.extend("MyClass",{
    getData: function() {
        this.showing = null;
        $.get("data.json",this.callback('gotData'),'json')
    },
    gotData: function( data ) {
        this.showing = data;
    }
},{});
MyClass.showData();

Currying Arguments

Additional arguments to callback will fill in arguments on the returning function.
$.Class.extend("MyClass",{
   getData: function( callback ) {
     $.get("data.json",this.callback('process',callback),'json');
   },
   process: function( callback, jsonData ) { //callback is added as first argument
       jsonData.processed = true;
       callback(jsonData);
   }
},{});
MyClass.getData(showDataFunc)

Nesting Functions

Callback can take an array of functions to call as the first argument. When the returned callback function is called each function in the array is passed the return value of the prior function. This is often used to eliminate currying initial arguments.
$.Class.extend("MyClass",{
   getData: function( callback ) {
     //calls process, then callback with value from process
     $.get("data.json",this.callback(['process2',callback]),'json') 
   },
   process2: function( type,jsonData ) {
       jsonData.processed = true;
       return [jsonData];
   }
},{});
MyClass.getData(showDataFunc);
$.Class.callback(funcs, fname) -> Function
{}
{String|Array}

If a string, it represents the function to be called.
If it is an array, it will call each function in order and pass the return value of the prior function to the next function.

{Function}

the callback function.

© Jupiter IT - JavaScriptMVC Training and Support