diff --git a/view/live/live.js b/view/live/live.js index 6567a1ab63a..9d6033ecf3d 100644 --- a/view/live/live.js +++ b/view/live/live.js @@ -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) { diff --git a/view/stache/stache_test.js b/view/stache/stache_test.js index 74fb6b1c7f6..8fba4a50ea8 100644 --- a/view/stache/stache_test.js +++ b/view/stache/stache_test.js @@ -3629,4 +3629,17 @@ steal("can/view/stache", "can/view","can/test","can/view/mustache/spec/specs",fu equal(frag.childNodes[0].outerHTML, expected, ' nodes added in proper position'); }); + + test('splicing negative indices works (#1038)', function() { + // http://jsfiddle.net/ZrWVQ/2/ + var template = '{{#each list}}

{{.}}

{{/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'); + }); });