diff options
author | Wilson Page <wilsonpage@me.com> | 2013-08-26 23:33:06 +0100 |
---|---|---|
committer | Wilson Page <wilsonpage@me.com> | 2013-08-26 23:33:06 +0100 |
commit | 79e948a924e29954184ec6af5afbf46651c3429e (patch) | |
tree | 21b65fd0105ff18b2e570ea8818d77be55becf88 | |
parent | d3c0846673a410c79fafacbb52d099ac136fd17d (diff) | |
download | fastdom-79e948a924e29954184ec6af5afbf46651c3429e.zip fastdom-79e948a924e29954184ec6af5afbf46651c3429e.tar.gz fastdom-79e948a924e29954184ec6af5afbf46651c3429e.tar.bz2 |
Build in callback context option
-rw-r--r-- | lib/dom-batch.js | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/lib/dom-batch.js b/lib/dom-batch.js index 1e1bcb4..2f80172 100644 --- a/lib/dom-batch.js +++ b/lib/dom-batch.js @@ -46,8 +46,8 @@ * @param {Function} fn * @api public */ - DomBatch.prototype.read = function(fn) { - this.reads.push(fn); + DomBatch.prototype.read = function(fn, ctx) { + add(this.reads, fn, ctx); this.request('read'); }; @@ -58,8 +58,8 @@ * @param {Function} fn * @api public */ - DomBatch.prototype.write = function(fn) { - this.writes.push(fn); + DomBatch.prototype.write = function(fn, ctx) { + add(this.writes, fn, ctx); this.request('write'); }; @@ -128,12 +128,22 @@ * Calls each job in * the list passed. * + * If a context has been + * stored on the function + * then it is used, else the + * current `this` is used. + * * @param {Array} list * @api private */ DomBatch.prototype.run = function(list) { + var fn; + var ctx; + while (list.length) { - list.shift().call(this); + fn = list.shift(); + ctx = fn._dbctx || this; + fn.call(ctx); } }; @@ -167,8 +177,35 @@ * Util */ - function remove(array, item) { - var index = array.indexOf(item); + /** + * Adds a function to + * the given array. + * + * If a context is given + * it is stored on the + * function object for + * later. + * + * @param {Array} array + * @param {Function} fn + * @param {Object} ctx + * @api private + */ + function add(array, fn, ctx) { + if (ctx) fn._dbctx = ctx; + array.push(fn); + } + + /** + * Removes a function + * from the given array. + * + * @param {Array} array + * @param {Function} item + * @api private + */ + function remove(array, fn) { + var index = array.indexOf(fn); if (~index) array.splice(index, 1); } |