Skip to content

Commit

Permalink
add batch function
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony MOI committed Oct 1, 2012
1 parent ac10870 commit 1abbb20
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/container.js
Expand Up @@ -60,6 +60,7 @@ CELL.container = function(spec, my) {
// protected
var load; /* load(); */
var refresh; /* refresh(); */
var batch; /* batch(fn, params); */


var that = CELL.emitter({});
Expand Down Expand Up @@ -106,11 +107,33 @@ CELL.container = function(spec, my) {
throw new Error('`load` must be implemented : ' + my.path);
};

/**
* Calls the given function for every children.
* @param fn can be a string representing the function name
* or a function(child, params)
* @param params
*/
batch = function() {
var args = Array.prototype.slice.call(arguments);
var fn = args.shift();

for(var c in my.children) {
if(my.children.hasOwnProperty(c)) {
if(typeof fn === 'string' && typeof my.children[c][fn] === 'function')
my.children[c][fn].apply(this, args);
else if(typeof fn === 'function') {
var child_args = [ my.children[c] ].concat(args);
fn.apply(this, child_args);
}
}
}
};


CELL.method(that, 'load', load, _super);
CELL.method(that, 'refresh', refresh, _super);
CELL.method(that, 'find', find, _super);
CELL.method(that, 'batch', batch, _super);

CELL.getter(that, 'children', my, 'children');
CELL.getter(that, 'name', my, 'name');
Expand Down

0 comments on commit 1abbb20

Please sign in to comment.