Skip to content

Commit

Permalink
Allow direct reference to an observable view model within a 'with' bi…
Browse files Browse the repository at this point in the history
…nding using $rawData
  • Loading branch information
Joe Lee-Moyet authored and Joe Lee-Moyet committed Sep 17, 2014
1 parent 7647e0d commit d59a69a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions spec/defaultBindings/withBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,20 @@ describe('Binding: With', function() {
viewModel.topitem(null);
expect(testNode).toContainHtml("hello <!-- ko with: topitem --><!-- /ko -->");
});

it('Should provide access to an observable viewModel through $rawData', function() {
testNode.innerHTML = "<div data-bind='with: item'><input data-bind='value: $rawData'/></div>";
var item = ko.observable('one');
ko.applyBindings({ item: item }, testNode);
expect(testNode.childNodes[0]).toHaveValues(['one']);

// Should update observable when input is changed
testNode.childNodes[0].childNodes[0].value = 'two';
ko.utils.triggerEvent(testNode.childNodes[0].childNodes[0], "change");
expect(item()).toEqual('two');

// Should update the input when the observable changes
item('three');
expect(testNode.childNodes[0]).toHaveValues(['three']);
});
});
2 changes: 1 addition & 1 deletion src/binding/defaultBindings/ifIfnotWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function makeWithIfBinding(bindingKey, isWith, isNot, makeContextCallback) {
if (!isFirstRender) {
ko.virtualElements.setDomNodeChildren(element, ko.utils.cloneNodes(savedNodes));
}
ko.applyBindingsToDescendants(makeContextCallback ? makeContextCallback(bindingContext, dataValue) : bindingContext, element);
ko.applyBindingsToDescendants(makeContextCallback ? makeContextCallback(bindingContext, valueAccessor) : bindingContext, element);
} else {
ko.virtualElements.emptyNode(element);
}
Expand Down

0 comments on commit d59a69a

Please sign in to comment.