#each .. in extends the data context for children

Regular #each ({{#each items}}) sets a new data context for each child view to
the item of the sequence.

The new #each ({{#each item in items}}) preserves the data context and extends
it to have the variable like 'item'
This commit is contained in:
Slava Kim
2015-01-26 15:02:42 -08:00
parent 4df9b115a5
commit 5796bd77d6

View File

@@ -112,19 +112,20 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
}, {
addedAt: function (id, item, index) {
Tracker.nonreactive(function () {
// create an alias for the data context defined by argFunc.
var newDataContext;
if (eachView.variableName) {
// new-style #each (as in {{#each item in items}})
// the new data context is the same but with an extension by
// variable name (e.g. 'item')
var _item = item;
if (! _.isObject(item)) {
item = {};
} else {
item = _.clone(item);
}
item[eachView.variableName] = _item;
var dataContext = Blaze.getData(eachView);
newDataContext = _.clone(dataContext) || {};
newDataContext[eachView.variableName] = item;
} else {
newDataContext = item;
}
var newItemView = Blaze.With(item, eachView.contentFunc);
var newItemView = Blaze.With(newDataContext, eachView.contentFunc);
eachView.numItems++;
if (eachView.expandedValueDep) {