diff --git a/mithril.js b/mithril.js index 2e1153bb..5ac0596b 100644 --- a/mithril.js +++ b/mithril.js @@ -442,11 +442,10 @@ var m = (function app(window, undefined) { function checkView(data, view, cached, cachedControllers, controllers, views) { var controller = getController(cached.views, view, cachedControllers, data.controller); - //Faster to coerce to number and check for NaN - var key = +(data && data.attrs && data.attrs.key); + var key = data && data.attrs && data.attrs.key; data = pendingRequests === 0 || forcing || cachedControllers && cachedControllers.indexOf(controller) > -1 ? data.view(controller) : {tag: "placeholder"}; if (data.subtree === "retain") return data; - if (key === key) (data.attrs = data.attrs || {}).key = key; + (data.attrs = data.attrs || {}).key = key; updateLists(views, controllers, view, controller); return data; } diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 06fda631..ec24f976 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -269,6 +269,41 @@ function testMithril(mock) { return firstBefore === firstAfter }) + test(function() { + //string keys in components should work + mock.requestAnimationFrame.$resolve() + + var root = mock.document.createElement("div") + var list = [1, 2, 3] + var component = { + controller: function() {}, + view: function() { + return list.map(function(i) { + return m.component(sub, {key: "key" + i}) + }) + } + } + var sub = { + controller: function() {}, + view: function() { + return m("div") + } + } + m.mount(root, component) + + var firstBefore = root.childNodes[0] + + mock.requestAnimationFrame.$resolve() + + list.reverse() + m.redraw(true) + + mock.requestAnimationFrame.$resolve() + + var firstAfter = root.childNodes[2] + + return firstBefore === firstAfter + }) test(function() { //keys in subcomponents should work mock.requestAnimationFrame.$resolve()