Skip to content

Commit

Permalink
Replace automatic inheritance of all bindingContext properties with n…
Browse files Browse the repository at this point in the history
…ew $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.
  • Loading branch information
SteveSanderson committed Feb 2, 2012
1 parent 8df7869 commit ab8087e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion spec/bindingAttributeBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
8 changes: 4 additions & 4 deletions src/binding/bindingAttributeSyntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ab8087e

Please sign in to comment.