From ab8087e330539c0a6be1b9f7ca49a761db4f1796 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 2 Feb 2012 12:50:29 +0000 Subject: [PATCH] Replace automatic inheritance of all bindingContext properties with new $parentContext variable Benefit #1: Can reach custom properties on *all* ancestor contexts, and are not limited to those that haven't been overwritten (e.g., if we had $index, would now be able to reach all ancestor $index values, not just the closest one) Benefit #2: Allows discrimination between properties at the current level and properties at the parent level, which is good because not all custom properties may make sense to inherit. --- spec/bindingAttributeBehaviors.js | 2 +- src/binding/bindingAttributeSyntax.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/bindingAttributeBehaviors.js b/spec/bindingAttributeBehaviors.js index 1605770da..b99035dbf 100644 --- a/spec/bindingAttributeBehaviors.js +++ b/spec/bindingAttributeBehaviors.js @@ -322,7 +322,7 @@ describe('Binding attribute syntax', { ko.applyBindings(null, testNode); value_of(ko.contextFor(testNode.childNodes[1].childNodes[0])).should_be(innerContext); value_of(ko.contextFor(testNode.childNodes[1].childNodes[1]).$parent).should_be(innerContext.$data); - value_of(ko.contextFor(testNode.childNodes[1].childNodes[1]).custom).should_be(true); + value_of(ko.contextFor(testNode.childNodes[1].childNodes[1]).$parentContext.custom).should_be(true); }, 'Should not reinvoke init for notifications triggered during first evaluation': function () { diff --git a/src/binding/bindingAttributeSyntax.js b/src/binding/bindingAttributeSyntax.js index e3454817e..eac7270ef 100755 --- a/src/binding/bindingAttributeSyntax.js +++ b/src/binding/bindingAttributeSyntax.js @@ -2,17 +2,17 @@ ko.bindingHandlers = {}; ko.bindingContext = function(dataItem, parentBindingContext) { + this['$data'] = dataItem; if (parentBindingContext) { - // copy all properties from parent binding context - ko.utils.extend(this, parentBindingContext); + this['$parentContext'] = parentBindingContext; this['$parent'] = parentBindingContext['$data']; - this['$parents'] = (this['$parents'] || []).slice(0); + this['$parents'] = (parentBindingContext['$parents'] || []).slice(0); this['$parents'].unshift(this['$parent']); + this['$root'] = parentBindingContext['$root']; } else { this['$parents'] = []; this['$root'] = dataItem; } - this['$data'] = dataItem; } ko.bindingContext.prototype['createChildContext'] = function (dataItem) { return new ko.bindingContext(dataItem, this);