Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first guess at how to support modifying observable indices.

  • Loading branch information...
commit 9be31b2f170aad60fd140ec6d31c57273097d1b3 1 parent 8de3398
@barkmadley authored
View
12 src/binding/editDetection/arrayToDomNodeChildren.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);
+ });
}
})();
View
2  src/templating/templating.js
@@ -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;
};
Please sign in to comment.
Something went wrong with that request. Please try again.