From fa4e20a935117ea470f1678a5f71d6cde9c8ec88 Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Wed, 10 Jul 2013 06:21:33 -0700 Subject: [PATCH] reactive observing #each --- examples/unfinished/shark/client/shark.js | 4 +-- packages/ui/each.js | 44 ++++++++++++----------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/examples/unfinished/shark/client/shark.js b/examples/unfinished/shark/client/shark.js index 0dc6f48c6b..f2bbde2537 100644 --- a/examples/unfinished/shark/client/shark.js +++ b/examples/unfinished/shark/client/shark.js @@ -1,13 +1,13 @@ Items = new Meteor.Collection(null); Items.insert({ text: 'Foo' }); Items.insert({ text: 'Bar' }); -Items.insert({ text: 'Baz' }); +Items.insert({ text: 'Beef' }); Meteor.startup(function () { Meteor.setTimeout(function () { Items.insert({ text: 'Qux' }); Items.remove({ text: 'Foo' }); - Items.update({ text: 'Bar' }, { text: 'Car' }); + Items.update({ text: 'Bar' }, { text: 'Coke' }); }, 1000); }); diff --git a/packages/ui/each.js b/packages/ui/each.js index ed2b6641ec..76d6b0fc7c 100644 --- a/packages/ui/each.js +++ b/packages/ui/each.js @@ -45,6 +45,9 @@ _UI.List = Component.extend({ this.lastNode().nextSibling, this.parentNode()); } }, + getItem: function (id) { + return this._items.get(id) || null; + }, render: function (buf) { var self = this; if (self._items.empty()) { @@ -65,15 +68,7 @@ _UI.List = Component.extend({ _UI.Each = Component.extend({ typeName: 'Each', - init: function () { - this.list = new _UI.List({_idStringify: Meteor.idStringify}); - this.elseView = this.elseContent(); - - // must be a cursor and isn't allowed to change - var cursor = this.data(); - - - }, + List: _UI.List, render: function (buf) { var self = this; @@ -83,7 +78,12 @@ _UI.Each = Component.extend({ var cursor = self.data(); // XXX support null - /* cursor.observe({ + var list = new self.List({ + _idStringify: Meteor.idStringify, + elseContent: self.elseContent + }); + + cursor.observe({ _no_indices: true, addedAt: function (doc, i, beforeId) { var comp = self.content(function () { @@ -96,17 +96,21 @@ _UI.Each = Component.extend({ // XXX could `before` be a falsy ID? Technically // idStringify seems to allow for them -- though // OrderedDict won't call stringify on a falsy arg. - items.putBefore(doc._id, comp, beforeId); - + list.addItemBefore(doc._id, comp, beforeId); + }, + removed: function (doc) { + list.removeItem(doc._id); + }, + movedTo: function (doc, i, j, beforeId) { + list.moveItemBefore(doc._id, beforeId); + }, + changed: function (newDoc) { + var comp = list.getItem(newDoc._id); + comp._data = newDoc; + comp.dataDep.changed(); } - });*/ + }); - //if (items.empty()) { -// buf(self.elseContent()); -// } else { -// items.forEach(function (comp) { -// buf(comp); -// }); -// } + buf(list); } });