From bed3d1ed3dcb9e4f77ffb2012715b5ed2f6efec5 Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Tue, 6 May 2014 08:59:47 -0700 Subject: [PATCH] Basic #each works Needs {{else}} case. Also {{.}} doesn't work yet --- packages/blaze/render.js | 2 ++ packages/spacebars-compiler/codegen2.js | 2 +- packages/spacebars/package.js | 1 + packages/spacebars/spacebars-runtime.js | 33 ++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/blaze/render.js b/packages/blaze/render.js index 1ef33aa8d9..a828869eaf 100644 --- a/packages/blaze/render.js +++ b/packages/blaze/render.js @@ -34,6 +34,8 @@ Blaze.renderList = function (funcSequence) { throw new Error("Expected a Blaze.Sequence of functions in " + "Blaze.renderList"); + //XXX var controller = Blaze.currentController; + var initialMembers; var computation = Deps.autorun(function (c) { if (! c.firstRun) diff --git a/packages/spacebars-compiler/codegen2.js b/packages/spacebars-compiler/codegen2.js index c8baefb109..beaf4572ac 100644 --- a/packages/spacebars-compiler/codegen2.js +++ b/packages/spacebars-compiler/codegen2.js @@ -10,7 +10,7 @@ var builtInBlockHelpers = SpacebarsCompiler._builtInBlockHelpers = { 'if': 'Blaze.If', 'unless': 'Blaze.Unless', 'with': 'Spacebars.With2', - 'each': 'UI.Each' + 'each': 'Spacebars.Each' }; // These must be prefixed with `UI.` when you use them in a template. diff --git a/packages/spacebars/package.js b/packages/spacebars/package.js index 93c2f50dc5..a8c454c381 100644 --- a/packages/spacebars/package.js +++ b/packages/spacebars/package.js @@ -16,5 +16,6 @@ Package.on_use(function (api) { api.use('htmljs'); api.use('ui'); + api.use('observe-sequence'); api.add_files(['spacebars-runtime.js']); }); diff --git a/packages/spacebars/spacebars-runtime.js b/packages/spacebars/spacebars-runtime.js index e85cf1cf8f..77fa60cfed 100644 --- a/packages/spacebars/spacebars-runtime.js +++ b/packages/spacebars/spacebars-runtime.js @@ -243,10 +243,41 @@ Spacebars.TemplateWith = function (argFunc, contentBlock) { }; Spacebars.With2 = function (argFunc, contentFunc, elseContentFunc) { - var data = Blaze.Var(argFunc); + var data = Blaze.Var(argFunc, UI.safeEquals); return Blaze.If(function () { return data.get(); }, function () { return Blaze.With(data, contentFunc); }, elseContentFunc); }; + +Spacebars.Each = function (argFunc, contentFunc, elseContentFunc) { + var seq = new Blaze.Sequence; + + var argVar = Blaze.Var(argFunc, UI.safeEquals); + ObserveSequence.observe(function () { + return argVar.get(); + }, { + addedAt: function (id, item, index) { + var dataVar = Blaze.Var(item, UI.safeEquals); + var func = function () { + return Blaze.With(dataVar, contentFunc); + }; + func.dataVar = dataVar; + seq.addItem(func, index); + }, + removedAt: function (id, item, index) { + seq.removeItem(index); + }, + changedAt: function (id, newItem, oldItem, index) { + seq.get(index).dataVar.set(newItem); + }, + movedTo: function (id, item, fromIndex, toIndex) { + var f = seq.get(fromIndex); + seq.removeItem(fromIndex); + seq.addItem(f, toIndex); + } + }); + + return Blaze.List(seq); +};