From 496d820ee3348ac4006d91095aac08d5c5e6dfd3 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Tue, 27 Jan 2015 20:53:58 -0500 Subject: [PATCH] #424 fix key association when DOM is modified by external code --- docs/change-log.md | 4 ++++ mithril.js | 14 ++++++++------ tests/mithril-tests.js | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/docs/change-log.md b/docs/change-log.md index 6424e92f..9e1a2180 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -5,11 +5,15 @@ ### News: - Calling m.module without a module now unloads the current one [#420](https://github.com/lhorie/mithril.js/issues/420) +- Both `controller` and `view` properties in modules are now optional ### Bug Fixes: - prevent empty class attributes [#382](https://github.com/lhorie/mithril.js/issues/382) - array-to-querystring serialization in `m.request` now behaves like jQuery [#426](https://github.com/lhorie/mithril.js/issues/426) +- fix querystring detection bug in pathname mode [#425](https://github.com/lhorie/mithril.js/issues/425) +- don't add history entry if reloading from a link [#428](https://github.com/lhorie/mithril.js/issues/428) +- fix key association when DOM order is modified by external code [#424](https://github.com/lhorie/mithril.js/issues/424) --- diff --git a/mithril.js b/mithril.js index cc557cbe..66eb04f8 100644 --- a/mithril.js +++ b/mithril.js @@ -133,7 +133,7 @@ var m = (function app(window, undefined) { for (var i = 0; i < cached.length; i++) { if (cached[i] && cached[i].attrs && cached[i].attrs.key != null) { shouldMaintainIdentities = true; - existing[cached[i].attrs.key] = {action: DELETION, index: i} + existing[cached[i].attrs.key] = {action: DELETION, index: i, element: cached[i].nodes[0]} } } if (shouldMaintainIdentities) { @@ -154,11 +154,13 @@ var m = (function app(window, undefined) { if (data[i].attrs.key != null) { var key = data[i].attrs.key; if (!existing[key]) existing[key] = {action: INSERTION, index: i}; - else existing[key] = { - action: MOVE, - index: i, - from: existing[key].index, - element: parentElement.childNodes[existing[key].index] || $document.createElement("div") + else { + existing[key] = { + action: MOVE, + index: i, + from: existing[key].index, + element: existing[key].element || $document.createElement("div") + } } } else unkeyed.push({index: i, element: parentElement.childNodes[i] || $document.createElement("div")}) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index cbf52d4d..2b3ce839 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -828,6 +828,21 @@ function testMithril(mock) { m.render(root, m("div", [console.log()])) //don't throw in Firefox return true }) + test(function() { + var root = mock.document.createElement("div") + m.render(root, [ + m("#div-1", {key: 1}), + m("#div-2", {key: 2}), + m("#div-3", {key: 3}) + ]) + root.appendChild(root.childNodes[1]) + m.render(root, [ + m("#div-1", {key: 1}), + m("#div-3", {key: 3}), + m("#div-2", {key: 2}) + ]) + return root.childNodes.map(function(node) {return node.id}).join() == "div-1,div-3,div-2" + }) //end m.render //m.redraw