Basic #each works

Needs {{else}} case.  Also {{.}} doesn't work yet
This commit is contained in:
David Greenspan
2014-05-06 08:59:47 -07:00
parent 1669783a86
commit bed3d1ed3d
4 changed files with 36 additions and 2 deletions

View File

@@ -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)

View File

@@ -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.

View File

@@ -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']);
});

View File

@@ -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);
};