Permalink
Browse files

first guess at how to support modifying observable indices.

  • Loading branch information...
1 parent 8de3398 commit 9be31b2f170aad60fd140ec6d31c57273097d1b3 @barkmadley committed Oct 22, 2011
Showing with 13 additions and 1 deletion.
  1. +12 −0 src/binding/editDetection/arrayToDomNodeChildren.js
  2. +1 −1 src/templating/templating.js
@@ -46,6 +46,7 @@
var newMappingResult = [];
var lastMappingResultIndex = 0;
var nodesToDelete = [];
+ var nodesToUpdateIndex = [];
var nodesAdded = [];
var insertAfterNode = null;
for (var i = 0, j = editScript.length; i < j; i++) {
@@ -57,6 +58,13 @@
if (dataToRetain.domNodes.length > 0)
insertAfterNode = dataToRetain.domNodes[dataToRetain.domNodes.length - 1];
lastMappingResultIndex++;
+ for (var nodeI = 0; nodeI < dataToRetain.domNodes.length; nodeI++) {
+ var node = dataToRetain.domNodes[nodeI];
+ var context = ko.storedBindingContextForNode(node);
+ if (context && context['$index']) {
+ nodesToUpdateIndex.push({ index: context['$index'], newIndex: i});
+ }
+ }
break;
case "deleted":
@@ -125,6 +133,10 @@
// Store a copy of the array items we just considered so we can difference it next time
ko.utils.domData.set(domNode, lastMappingResultDomDataKey, newMappingResult);
+
+ ko.utils.arrayForEach(nodesToUpdateIndex, function(obj) {
+ obj.index(obj.newIndex);
+ });
}
})();
@@ -119,7 +119,7 @@
ko.renderTemplateForEach = function (template, arrayOrObservableArray, options, targetNode, parentBindingContext) {
var createInnerBindingContext = function(arrayValue, index) {
var result = parentBindingContext.createChildContext(ko.utils.unwrapObservable(arrayValue));
- result['$index'] = index;
+ result['$index'] = ko.observable(index);
return result;
};

0 comments on commit 9be31b2

Please sign in to comment.