From c81294bce8d76efe88576ffa7458640aaad94fed Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Tue, 24 Feb 2015 17:53:49 -0500 Subject: [PATCH] #466 allow modules as second parameter of `m` --- mithril.js | 6 +++--- tests/mithril-tests.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/mithril.js b/mithril.js index f9debb7e..297bb3bb 100644 --- a/mithril.js +++ b/mithril.js @@ -33,7 +33,7 @@ var m = (function app(window, undefined) { */ function m() { var args = [].slice.call(arguments); - var hasAttrs = args[1] != null && type.call(args[1]) === OBJECT && !("tag" in args[1]) && !("subtree" in args[1]); + var hasAttrs = args[1] != null && type.call(args[1]) === OBJECT && !("tag" in args[1] || "view" in args[1]) && !("subtree" in args[1]); var attrs = hasAttrs ? args[1] : {}; var classAttrName = "class" in attrs ? "class" : "className"; var cell = {tag: "div", attrs: {}}; @@ -58,7 +58,7 @@ var m = (function app(window, undefined) { else { cell.children = children } - + for (var attrName in attrs) { if (attrName === classAttrName) { if (attrs[attrName] !== "") cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName]; @@ -232,7 +232,7 @@ var m = (function app(window, undefined) { } } else if (data != null && dataType === OBJECT) { - if (data.controller) { + if (data.view) { var module = data var controller = cached.controller || new (module.controller || function() {}) if (controller.onunload) unloaders.push({controller: controller, handler: controller.onunload}) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 0e4d21a7..3321f8b0 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -424,7 +424,44 @@ function testMithril(mock) { return loaded === false }) + test(function() { + // nested modules under keyed components should render + mock.requestAnimationFrame.$resolve() + var root = mock.document.createElement("div") + var count = 0 + var App = { + controller: function() {}, + view: function(ctrl) { + return m('.outer', [ + m('.inner', m.module(CommentList, { list: [1, 2, 3] })) + ]) + } + } + var CommentList = { + controller: function() {}, + view: function(ctrl, props) { + return m('.list', props.list.map(function(i) { + return m('.comment', [ + m.module(Reply, {key: i}) + ]) + })) + } + } + var Reply = { + controller: function() {}, + view: function() { + count++ + return m(".reply") + } + } + m.module(root, App) + + mock.requestAnimationFrame.$resolve() + + return count === 3 + }) + //m.withAttr test(function() { var value