Skip to content

Commit

Permalink
When using preProcessNodes within foreach, ensure that updates are ha…
Browse files Browse the repository at this point in the history
…ndled correctly
  • Loading branch information
mbest committed Jul 2, 2013
1 parent 148fd32 commit e30410c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 14 additions & 1 deletion spec/bindingAttributeBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,22 @@ describe('Binding attribute syntax', function() {
};

testNode.innerHTML = "<prop1></prop1>, <p data-bind='text: prop2'></p>, prefix {{ prop3 }} suffix<div data-bind='foreach: prop4'> {{ prop3 }} x</div>";
ko.applyBindings({ prop1: 'PROP1VAL', prop2: 'PROP2VAL', prop3: 'PROP3VAL', prop4: [ {prop3: 'PROP4VAL1'}, {prop3: 'PROP4VAL2'} ] }, testNode);
var vm = {
prop1: 'PROP1VAL',
prop2: 'PROP2VAL',
prop3: ko.observable('PROP3VAL'),
prop4: ko.observable([ {prop3: 'PROP4VAL1'}, {prop3: 'PROP4VAL2'} ])
};
ko.applyBindings(vm, testNode);
expect(testNode).toContainText('PROP1VAL, PROP2VAL, prefix PROP3VAL suffix PROP4VAL1 x PROP4VAL2 x');

// Update observables and test that DOM is updated
vm.prop3('NEW_PROP3VAL');
expect(testNode).toContainText('PROP1VAL, PROP2VAL, prefix NEW_PROP3VAL suffix PROP4VAL1 x PROP4VAL2 x');

vm.prop4([ {prop3: 'PROP4VAL3'}, {prop3: 'PROP4VAL4'} ]);
expect(testNode).toContainText('PROP1VAL, PROP2VAL, prefix NEW_PROP3VAL suffix PROP4VAL3 x PROP4VAL4 x');

ko.bindingProvider.instance = originalBindingProvider;
});
});
6 changes: 6 additions & 0 deletions src/templating/templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
lastNode = newNodes[newNodes.length-1];
}
});
// Because preProcessNode can change the nodes, including the first and last nodes, replace
// continuousNodeArray with the latest first/last nodes (we don't actually need the inner nodes)
continuousNodeArray.length = 0;
continuousNodeArray.push(firstNode);
if (lastNode !== firstNode)
continuousNodeArray.push(lastNode);
}

// Need to applyBindings *before* unmemoziation, because unmemoization might introduce extra nodes (that we don't want to re-bind)
Expand Down

0 comments on commit e30410c

Please sign in to comment.