From 149c0cdaefac441d2bd56b4daafa54d546905d3e Mon Sep 17 00:00:00 2001 From: nickolasgregory Date: Mon, 15 Sep 2014 19:52:35 +1000 Subject: [PATCH 1/5] docs: minor typo --- docs/mithril.deferred.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/mithril.deferred.md b/docs/mithril.deferred.md index 902de6af..2f6166e4 100644 --- a/docs/mithril.deferred.md +++ b/docs/mithril.deferred.md @@ -120,7 +120,7 @@ var greetAsync = function() { ### Differences from Promises/A+ For the most part, Mithril promises behave as you'd expect a [Promise/A+](http://promises-aplus.github.io/promises-spec/) promise to behave, but has one difference: -Mithril promises atempt to execute synchronously if possible. +Mithril promises attempt to execute synchronously if possible. To illustrate the difference between Mithril and A+ promises, consider the code below: From 46e8ffc627492d3dec6f42d0673a0672fefe6727 Mon Sep 17 00:00:00 2001 From: nickolasgregory Date: Mon, 15 Sep 2014 20:01:14 +1000 Subject: [PATCH 2/5] tweaked example components setter-getter Pass the function, not the returned object, to the autocompleter component. Allows `dashboard` to pass results from `m.request()` - Tested OK --- docs/components.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/components.md b/docs/components.md index 712cfe5e..bb73900f 100644 --- a/docs/components.md +++ b/docs/components.md @@ -99,7 +99,7 @@ autocompleter.controller = function(data, getter) { this.change = function(value) { this.value(value); - var list = value === "" ? [] : data.filter(function(item) { + var list = value === "" ? [] : data().filter(function(item) { return this.getter(item).toLowerCase().indexOf(value.toLowerCase()) > -1; }, this); this.data(list); @@ -130,7 +130,7 @@ var dashboard = {} dashboard.controller = function() { this.names = m.prop([{id: 1, name: "John"}, {id: 2, name: "Bob"}, {id: 2, name: "Mary"}]); - this.autocompleter = new autocompleter.controller(this.names(), function(item) { + this.autocompleter = new autocompleter.controller(this.names, function(item) { return item.name; }); }; From 5a134ecf5bfb513acbd0607e5ed9535593183f2a Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Mon, 15 Sep 2014 15:53:52 -0400 Subject: [PATCH 3/5] fix bug when mixing keyed and unkeyed elements --- mithril.js | 4 ++-- tests/mithril-tests.js | 11 +++++++++++ tests/mock.js | 6 +++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/mithril.js b/mithril.js index 1946fd77..569b3c70 100644 --- a/mithril.js +++ b/mithril.js @@ -118,7 +118,7 @@ Mithril = m = new function app(window, undefined) { 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]} } - else unkeyed.push({index: i, element: parentElement.childNodes[i]}) + else unkeyed.push({index: i, element: parentElement.childNodes[i] || window.document.createElement("dummy")}) } } var actions = Object.keys(existing).map(function(key) {return existing[key]}) @@ -131,7 +131,7 @@ Mithril = m = new function app(window, undefined) { newCached.splice(change.index, 1) } if (change.action == INSERTION) { - var dummy = window.document.createElement("div") + var dummy = window.document.createElement("dummy2") dummy.key = data[change.index].attrs.key parentElement.insertBefore(dummy, parentElement.childNodes[change.index]) newCached.splice(change.index, 0, {attrs: {key: data[change.index].attrs.key}, nodes: [dummy]}) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 3159b8bb..e70e1bdc 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -584,6 +584,17 @@ function testMithril(mock) { var fifthAfter = root.childNodes[1] return firstBefore === firstAfter && secondBefore === secondAfter && thirdBefore === thirdAfter && fourthBefore === fourthAfter && fifthBefore === fifthAfter }) + test(function() { + //https://github.com/lhorie/mithril.js/issues/246 + //insert at beginning with non-keyed in the middle + var root = mock.document.createElement("div") + m.render(root, [m("a", {key: 1})]) + var firstBefore = root.childNodes[0] + m.render(root, [m("a", {key: 2}), m("br"), m("a", {key: 1})]) + var firstAfter = root.childNodes[2] + console.log(root.childNodes) + return firstBefore == firstAfter && root.childNodes[0].key == 2 && root.childNodes.length == 3 + }) test(function() { //https://github.com/lhorie/mithril.js/issues/134 var root = mock.document.createElement("div") diff --git a/tests/mock.js b/tests/mock.js index fae3574e..660f08f4 100644 --- a/tests/mock.js +++ b/tests/mock.js @@ -15,7 +15,11 @@ mock.window = new function() { insertBefore: function(node, reference) { node.parentNode = this var referenceIndex = this.childNodes.indexOf(reference) - if (referenceIndex < 0) this.childNodes.push(node) + if (referenceIndex < 0) { + var index = this.childNodes.indexOf(node) + if (index > -1) this.childNodes.splice(index, 1) + this.childNodes.push(node) + } else { var index = this.childNodes.indexOf(node) if (index > -1) this.childNodes.splice(index, 1) From b2245061597bfec69912e4b49565ee4685e2156f Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Mon, 15 Sep 2014 17:29:01 -0400 Subject: [PATCH 4/5] update change log --- docs/change-log.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/change-log.md b/docs/change-log.md index c32a8b7c..7347af50 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -13,6 +13,7 @@ - gracefully degrade on IE exceptions when setting invalid values - fixes for Typescript definition file +- fixed bug in keys algorithm when mixing keyed and unkeyed elements [#246](https://github.com/lhorie/mithril.js/issues/246) --- From f667d18b36b13d4a0ddb2f92d854febcaeec049b Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Mon, 15 Sep 2014 21:30:52 -0400 Subject: [PATCH 5/5] don't use invalid element tag names --- mithril.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mithril.js b/mithril.js index 569b3c70..49b0e48a 100644 --- a/mithril.js +++ b/mithril.js @@ -118,7 +118,7 @@ Mithril = m = new function app(window, undefined) { 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]} } - else unkeyed.push({index: i, element: parentElement.childNodes[i] || window.document.createElement("dummy")}) + else unkeyed.push({index: i, element: parentElement.childNodes[i] || window.document.createElement("div")}) } } var actions = Object.keys(existing).map(function(key) {return existing[key]}) @@ -131,7 +131,7 @@ Mithril = m = new function app(window, undefined) { newCached.splice(change.index, 1) } if (change.action == INSERTION) { - var dummy = window.document.createElement("dummy2") + var dummy = window.document.createElement("div") dummy.key = data[change.index].attrs.key parentElement.insertBefore(dummy, parentElement.childNodes[change.index]) newCached.splice(change.index, 0, {attrs: {key: data[change.index].attrs.key}, nodes: [dummy]})