From 3b3bf33dc80ccac930b19ea75204301134812832 Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Wed, 10 Jul 2013 04:46:34 -0700 Subject: [PATCH] each in own file --- packages/ui/components.js | 41 ------------------------- packages/ui/each.js | 63 +++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 76 deletions(-) diff --git a/packages/ui/components.js b/packages/ui/components.js index 0e894e0a31..c31474f1a5 100644 --- a/packages/ui/components.js +++ b/packages/ui/components.js @@ -34,47 +34,6 @@ _UI.If = Component.extend({ } }); -_UI.Each = Component.extend({ - typeName: 'Each', - render: function (buf) { - var self = this; - - // XXX support arrays too. - // XXX and objects. - // For now, we assume the data is a database cursor. - var cursor = self.data(); - // XXX support null - - // id -> component - var items = self.items = new OrderedDict(Meteor.idStringify); - - cursor.observe({ - _no_indices: true, - addedAt: function (doc, i, beforeId) { - var comp = self.content(function () { - this.dataDep.depend(); - return this._data; - }, { - _data: doc, - dataDep: new Deps.Dependency - }); - // 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); - } - }); - - if (items.empty()) { - buf(self.elseContent()); - } else { - items.forEach(function (comp) { - buf(comp); - }); - } - } -}); - _UI.Counter = Component.extend({ typeName: "Counter", fields: { diff --git a/packages/ui/each.js b/packages/ui/each.js index 5379aba76a..708db1a619 100644 --- a/packages/ui/each.js +++ b/packages/ui/each.js @@ -6,44 +6,37 @@ _UI.Each = Component.extend({ var self = this; // XXX support arrays too. + // XXX and objects. // For now, we assume the data is a database cursor. var cursor = self.data(); - // XXX also support `null` + // XXX support null - + // id -> component + var items = self.items = new OrderedDict(Meteor.idStringify); - var self = this; - buf(self.content(function () { return 0; })), - buf(self.content(function () { return 1; })); - buf(self.content(function () { return 2; })); + cursor.observe({ + _no_indices: true, + addedAt: function (doc, i, beforeId) { + var comp = self.content(function () { + this.dataDep.depend(); + return this._data; + }, { + _data: doc, + dataDep: new Deps.Dependency + }); + // 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); + } + }); + + if (items.empty()) { + buf(self.elseContent()); + } else { + items.forEach(function (comp) { + buf(comp); + }); + } } }); - - -// Function equal to LocalCollection._idStringify, or the identity -// function if we don't have LiveData. Converts item keys (i.e. DDP -// keys) to strings for storage in an OrderedDict. -var idStringify; - -// XXX not clear if this is the right way to do a weak dependency -// now, post-linker -if (typeof LocalCollection !== 'undefined') { - idStringify = function (id) { - if (id === null) - return id; - else - return LocalCollection._idStringify(id); - }; -} else { - idStringify = function (id) { return id; }; -} - -// XXX duplicated code from minimongo.js. -var applyChanges = function (doc, changeFields) { - _.each(changeFields, function (value, key) { - if (value === undefined) - delete doc[key]; - else - doc[key] = value; - }); -};