From 5796bd77d6de3bb09a83bd0ed553b6475a8642bd Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Mon, 26 Jan 2015 15:02:42 -0800 Subject: [PATCH] #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' --- packages/blaze/builtins.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/blaze/builtins.js b/packages/blaze/builtins.js index fc03136c0d..a92f273cd9 100644 --- a/packages/blaze/builtins.js +++ b/packages/blaze/builtins.js @@ -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) {