Skip to content

Commit

Permalink
Add third parameter to ko.applyBindings for modifying the root context
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Apr 15, 2017
1 parent 9798f6d commit a32bd19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
21 changes: 18 additions & 3 deletions spec/bindingAttributeBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Binding attribute syntax', function() {
didInit = true;
}
};
testNode.innerHTML = "<div id='testElement' data-bind='test:123'></div>";
testNode.innerHTML = "<div id='testElement' data-bind='test'></div>";
ko.applyBindings(suppliedViewModel);
expect(didInit).toEqual(true);
});
Expand All @@ -44,17 +44,32 @@ describe('Binding attribute syntax', function() {
didInit = true;
}
};
testNode.innerHTML = "<div id='testElement' data-bind='test:123'></div>";
testNode.innerHTML = "<div id='testElement' data-bind='test'></div>";

var shouldNotMatchNode = document.createElement("DIV");
shouldNotMatchNode.innerHTML = "<div id='shouldNotMatchThisElement' data-bind='test:123'></div>";
shouldNotMatchNode.innerHTML = "<div id='shouldNotMatchThisElement' data-bind='test'></div>";
document.body.appendChild(shouldNotMatchNode);
this.after(function () { document.body.removeChild(shouldNotMatchNode); });

ko.applyBindings(suppliedViewModel, testNode);
expect(didInit).toEqual(true);
});

it('applyBindings should accept three parameters and use the third parameter as a callback for modifying the root context', function() {
var didInit = false;
ko.bindingHandlers.test = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
expect(bindingContext.extraValue).toEqual("extra");
didInit = true;
}
};
testNode.innerHTML = "<div id='testElement' data-bind='test'></div>";
ko.applyBindings(null, testNode, function(context) {
context.extraValue = "extra";
});
expect(didInit).toEqual(true);
});

it('Should tolerate empty or only white-space binding strings', function() {
testNode.innerHTML = "<div data-bind=''></div><div data-bind=' '></div>";
ko.applyBindings(null, testNode); // No exception means success
Expand Down
8 changes: 4 additions & 4 deletions src/binding/bindingAttributeSyntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,10 @@
return bindingInfo && bindingInfo.context;
}

function getBindingContext(viewModelOrBindingContext) {
function getBindingContext(viewModelOrBindingContext, extendContextCallback) {
return viewModelOrBindingContext && (viewModelOrBindingContext instanceof ko.bindingContext)
? viewModelOrBindingContext
: new ko.bindingContext(viewModelOrBindingContext);
: new ko.bindingContext(viewModelOrBindingContext, undefined, undefined, extendContextCallback);
}

ko.applyBindingAccessorsToNode = function (node, bindings, viewModelOrBindingContext) {
Expand All @@ -421,7 +421,7 @@
applyBindingsToDescendantsInternal(getBindingContext(viewModelOrBindingContext), rootNode, true);
};

ko.applyBindings = function (viewModelOrBindingContext, rootNode) {
ko.applyBindings = function (viewModelOrBindingContext, rootNode, extendContextCallback) {
// If jQuery is loaded after Knockout, we won't initially have access to it. So save it here.
if (!jQueryInstance && window['jQuery']) {
jQueryInstance = window['jQuery'];
Expand All @@ -437,7 +437,7 @@
throw Error("ko.applyBindings: first parameter should be your view model; second parameter should be a DOM node");
}

applyBindingsToNodeAndDescendantsInternal(getBindingContext(viewModelOrBindingContext), rootNode, true);
applyBindingsToNodeAndDescendantsInternal(getBindingContext(viewModelOrBindingContext, extendContextCallback), rootNode, true);
};

// Retrieving binding context from arbitrary nodes
Expand Down

0 comments on commit a32bd19

Please sign in to comment.