Skip to content

Commit

Permalink
Should update "if" binding before descendant bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Apr 15, 2017
1 parent 941a1f2 commit 992bc71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
34 changes: 22 additions & 12 deletions spec/asyncBindingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,7 @@ describe("Deferred bindings", function() {

jasmine.Clock.tick(1);
expect(testNode.childNodes[0]).toContainHtml('<span data-bind="text: childprop">moving child</span><span data-bind="text: childprop">first child</span><span data-bind="text: childprop">second child</span>');
expect(testNode.childNodes[0].childNodes[targetIndex]).not.toBe(itemNode); // node was create anew so it's not the same
});

// Spec fails due to changes from #1835 (is it important to try to fix this?)
xit('Should not throw an exception for value binding on multiple select boxes', function() {
testNode.innerHTML = "<select data-bind=\"options: ['abc','def','ghi'], value: x\"></select><select data-bind=\"options: ['xyz','uvw'], value: x\"></select>";
var observable = ko.observable();
expect(function() {
ko.applyBindings({ x: observable }, testNode);
jasmine.Clock.tick(1);
}).not.toThrow();
expect(observable()).not.toBeUndefined(); // The spec doesn't specify which of the two possible values is actually set
expect(testNode.childNodes[0].childNodes[targetIndex]).not.toBe(itemNode); // node was created anew so it's not the same
});

it('Should get latest value when conditionally included', function() {
Expand All @@ -161,4 +150,25 @@ describe("Deferred bindings", function() {
jasmine.Clock.tick(1);
expect(testNode.childNodes[0]).toContainHtml('<div data-bind="text: status">ok</div>');
});

it('Should update "if" binding before descendant bindings', function() {
// Based on example at http://stackoverflow.com/q/43341484/1287183
testNode.innerHTML = '<div data-bind="if: hasAddress()"><div data-bind="text: address().street"></div></div>';
var vm = {
address: ko.observable(),
hasAddress: ko.pureComputed(function () { return vm.address() != null; })
};

ko.applyBindings(vm, testNode);
jasmine.Clock.tick(1);
expect(testNode.childNodes[0]).toContainHtml('');

vm.address({street: '123 my street'});
jasmine.Clock.tick(1);
expect(testNode.childNodes[0]).toContainHtml('<div data-bind="text: address().street">123 my street</div>');

vm.address(null);
jasmine.Clock.tick(1);
expect(testNode.childNodes[0]).toContainHtml('');
});
});
2 changes: 1 addition & 1 deletion src/subscribables/dependentObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ var computedFn = {
}
},
subscribeToDependency: function (target) {
if (target._deferUpdates && !this[computedState].disposeWhenNodeIsRemoved) {
if (target._deferUpdates) {
var dirtySub = target.subscribe(this.markDirty, this, 'dirty'),
changeSub = target.subscribe(this.respondToChange, this);
return {
Expand Down

0 comments on commit 992bc71

Please sign in to comment.