Skip to content

Commit

Permalink
Merge pull request canjs#1312 from bitovi/live-negative-indices-1038
Browse files Browse the repository at this point in the history
Allow removing elements with negative indices from live lists
  • Loading branch information
daffl committed Nov 7, 2014
2 parents 79d21b9 + 7be06bc commit 831bbf5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion view/live/live.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ steal('can/util', 'can/view/elements.js', 'can/view', 'can/view/node_lists', 'ca
if (!duringTeardown && data.teardownCheck(text.parentNode)) {
return;
}
var removedMappings = masterNodeList.splice(index+1, items.length),
if(index < 0) {
index = indexMap.length + index;
}

var removedMappings = masterNodeList.splice(index + 1, items.length),
itemsToRemove = [];
can.each(removedMappings, function (nodeList) {

Expand Down
13 changes: 13 additions & 0 deletions view/stache/stache_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3629,4 +3629,17 @@ steal("can/view/stache", "can/view","can/test","can/view/mustache/spec/specs",fu

equal(frag.childNodes[0].outerHTML, expected, '<col> nodes added in proper position');
});

test('splicing negative indices works (#1038)', function() {
// http://jsfiddle.net/ZrWVQ/2/
var template = '{{#each list}}<p>{{.}}</p>{{/each}}';
var list = new can.List(['a', 'b', 'c', 'd']);
var frag = can.stache(template)({
list: list
});
var children = frag.childNodes.length;

list.splice(-1);
equal(frag.childNodes.length, children - 1, 'Child node removed');
});
});

0 comments on commit 831bbf5

Please sign in to comment.