diff --git a/mithril.js b/mithril.js index 144dd80f..e0bdaa77 100644 --- a/mithril.js +++ b/mithril.js @@ -244,7 +244,7 @@ var m = (function app(window, undefined) { var controllerIndex = m.redraw.strategy() == "diff" && cached.controllerConstructors ? cached.controllerConstructors.indexOf(controllerConstructor) : -1 var controller = controllerIndex > -1 ? cached.controllers[controllerIndex] : new (data.controller || function() {}) var key = data && data.attrs && data.attrs.key - data = pendingRequests == 0 || cached ? data.view(controller) : {tag: "placeholder"} + data = pendingRequests == 0 || (cached && cached.controllers) ? data.view(controller) : {tag: "placeholder"} if (key) { if (!data.attrs) data.attrs = {} data.attrs.key = key diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 437f7b33..8177c797 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -969,7 +969,7 @@ function testMithril(mock) { test(function() { //https://github.com/lhorie/mithril.js/issues/551 var root = mock.document.createElement("div") - var a = false, found = false, unloaded = false + var a = false, found = false, unloaded = false, redraws = 0 var Root = { view: function() { return Comp @@ -977,6 +977,7 @@ function testMithril(mock) { } var Comp = { view: function() { + redraws++ return m("div", {config: Comp.config}, [ m("div", {onclick: function() { a = !a @@ -1000,12 +1001,12 @@ function testMithril(mock) { mock.requestAnimationFrame.$resolve() - return !unloaded && found.id === "a" + return !unloaded && found.id === "a" && redraws == 3 }) test(function() { //https://github.com/lhorie/mithril.js/issues/551 var root = mock.document.createElement("div") - var a = false, found = false, unloaded = false + var a = false, found = false, unloaded = false, redraws = 0 var Root = { view: function() { return Comp @@ -1013,6 +1014,7 @@ function testMithril(mock) { } var Comp = { view: function() { + redraws++ return m("div", {config: Comp.config}, [ m("div", {onclick: function() { a = !a @@ -1037,7 +1039,26 @@ function testMithril(mock) { mock.requestAnimationFrame.$resolve() - return !unloaded && found.id === "a" + return !unloaded && found.id === "a" && redraws == 2 + }) + test(function() { + var root = mock.document.createElement("div") + var redraws = 0 + var Root = { + view: function() { + redraws++ + return m("div", {onclick: function() {m.redraw(true)}}) + } + } + + m.mount(root, Root) + + var target = root.childNodes[0] + target.onclick({currentTarget: target}) + + mock.requestAnimationFrame.$resolve() + + return redraws == 3 }) test(function() { //https://github.com/lhorie/mithril.js/issues/555 @@ -1081,6 +1102,82 @@ function testMithril(mock) { return root.childNodes[0].childNodes[2].childNodes[0].nodeValue == "Bob" }) + test(function() { + var root = mock.document.createElement("div") + var redraws = 0, data + var Root = { + view: function() { + return Comp + } + } + + var Comp = { + controller: function() { + this.foo = m.request({method: "GET", url: "/foo"}) + }, + view: function(ctrl) { + redraws++ + data = ctrl.foo() + return m("div") + } + } + + m.mount(root, Root) + + mock.requestAnimationFrame.$resolve() + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + + mock.requestAnimationFrame.$resolve() + m.mount(root, null) + mock.requestAnimationFrame.$resolve() + + return redraws == 1 && data.url == "/foo" + }) + test(function() { + var root = mock.document.createElement("div") + var redraws1 = 0, redraws2 = 0 + var Root = { + view: function() { + return m("div", [ + Comp1, + Comp2 + ]) + } + } + + var Comp1 = { + controller: function() { + this.foo = m.request({method: "GET", url: "/foo"}) + }, + view: function(ctrl) { + redraws1++ + return m("div") + } + } + var Comp2 = { + controller: function() { + this.bar = m.request({method: "GET", url: "/bar"}) + }, + view: function(ctrl) { + redraws2++ + return m("div") + } + } + + m.mount(root, Root) + + mock.requestAnimationFrame.$resolve() + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + + mock.requestAnimationFrame.$resolve() + mock.XMLHttpRequest.$instances.pop().onreadystatechange() + + mock.requestAnimationFrame.$resolve() + m.mount(root, null) + mock.requestAnimationFrame.$resolve() + + return redraws1 == 1 && redraws2 == 1 + }) m.redraw.strategy(undefined) //teardown for m.mount tests //m.withAttr