From 4d1689bb4df5260a0985bd423b9a100d7cd718a3 Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Wed, 11 Apr 2012 15:14:53 -0700 Subject: [PATCH] liveui: shorten CallbackReceiver code --- packages/liveui/liveui.js | 60 ++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/packages/liveui/liveui.js b/packages/liveui/liveui.js index 8efb4dda3a..4e73791bfc 100644 --- a/packages/liveui/liveui.js +++ b/packages/liveui/liveui.js @@ -296,54 +296,42 @@ Meteor.ui = Meteor.ui || {}; }; var CallbackReceiver = function() { - this.queue = []; - this.deps = {}; + var self = this; - _.bindAll(this); // make callbacks work even if copied + self.queue = []; + self.deps = {}; + + // attach these callback funcs to each instance, as they may + // not be called as methods by livedata. + _.each(["added", "removed", "moved", "changed"], function (name) { + self[name] = function (/* arguments */) { + self.queue.push([name].concat(_.toArray(arguments))); + self.signal(); + }; + }); }; Meteor.ui._CallbackReceiver = CallbackReceiver; - CallbackReceiver.prototype.added = function(doc, before_idx) { - this.queue.push(['added', doc, before_idx]); - this.signal(); - }; - CallbackReceiver.prototype.removed = function(doc, at_idx) { - this.queue.push(['removed', doc, at_idx]); - this.signal(); - }; - CallbackReceiver.prototype.moved = function(doc, old_idx, new_idx) { - this.queue.push(['moved', doc, old_idx, new_idx]); - this.signal(); - }; - CallbackReceiver.prototype.changed = function(doc, at_idx) { - this.queue.push(['changed', doc, at_idx]); - this.signal(); - }; CallbackReceiver.prototype.flush_to = function(t) { // fire all queued events on new target - for(var i=0; i