From 16624569ecf34337b17e9222b31201265be04a15 Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Tue, 9 Jul 2013 19:06:49 -0700 Subject: [PATCH] start of real (cursor-based) Each --- examples/unfinished/shark/client/shark.js | 4 +- packages/ui/components.js | 10 ----- packages/ui/each.js | 49 +++++++++++++++++++++++ packages/ui/library.js | 2 +- packages/ui/package.js | 1 + 5 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 packages/ui/each.js diff --git a/examples/unfinished/shark/client/shark.js b/examples/unfinished/shark/client/shark.js index 227badbbe3..e5beb4656f 100644 --- a/examples/unfinished/shark/client/shark.js +++ b/examples/unfinished/shark/client/shark.js @@ -104,13 +104,13 @@ Either = UIComponent.extend({ elseContent: Span }), { type: Span }); - buf(new _UI.Each({ + /* buf(new _UI.Each({ content: UIComponent.extend({ render: function (buf) { buf("
Each ", String(this.data()), "
"); } }) - })); + }));*/ } }); diff --git a/packages/ui/components.js b/packages/ui/components.js index dc0751a278..c31474f1a5 100644 --- a/packages/ui/components.js +++ b/packages/ui/components.js @@ -34,16 +34,6 @@ _UI.If = Component.extend({ } }); -_UI.Each = Component.extend({ - typeName: 'Each', - render: function (buf) { - var self = this; - buf(self.content(function () { return 0; })), - buf(self.content(function () { return 1; })); - buf(self.content(function () { return 2; })); - } -}); - _UI.Counter = Component.extend({ typeName: "Counter", fields: { diff --git a/packages/ui/each.js b/packages/ui/each.js new file mode 100644 index 0000000000..5379aba76a --- /dev/null +++ b/packages/ui/each.js @@ -0,0 +1,49 @@ +var Component = UIComponent; + +_UI.Each = Component.extend({ + typeName: 'Each', + render: function (buf) { + var self = this; + + // XXX support arrays too. + // For now, we assume the data is a database cursor. + var cursor = self.data(); + // XXX also support `null` + + + + var self = this; + buf(self.content(function () { return 0; })), + buf(self.content(function () { return 1; })); + buf(self.content(function () { return 2; })); + } +}); + + +// 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; + }); +}; diff --git a/packages/ui/library.js b/packages/ui/library.js index ffec3d572f..796192dd10 100644 --- a/packages/ui/library.js +++ b/packages/ui/library.js @@ -182,7 +182,7 @@ Each = Component.extend({ var idStringify; // XXX not clear if this is the right way to do a weak dependency -// now, on the linker branch +// now, post-linker if (typeof LocalCollection !== 'undefined') { idStringify = function (id) { if (id === null) diff --git a/packages/ui/package.js b/packages/ui/package.js index 91eacd0ace..c2be5c2a2e 100644 --- a/packages/ui/package.js +++ b/packages/ui/package.js @@ -20,6 +20,7 @@ Package.on_use(function (api) { 'render.js', 'dom.js', 'forms.js', + 'each.js', 'components.js', 'component.js', 'renderbuffer.js',