#466 allow modules as second parameter of m

This commit is contained in:
Leo Horie 2015-02-24 17:53:49 -05:00
parent 971b0def51
commit c81294bce8
2 changed files with 40 additions and 3 deletions

View file

@ -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})

View file

@ -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