From 655e4ceafbfae8ff4bc1486c250fbf31838c98c9 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Tue, 24 Feb 2015 21:25:39 -0500 Subject: [PATCH] allow omitting controller for sub modules --- mithril.js | 4 ++-- tests/mithril-tests.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mithril.js b/mithril.js index 297bb3bb..b8b81407 100644 --- a/mithril.js +++ b/mithril.js @@ -508,7 +508,7 @@ var m = (function app(window, undefined) { var FRAME_BUDGET = 16; //60 frames per second = 1 call per 16 ms function submodule(module, args) { var controller = function() { - return module.controller.apply(this, args) || this + return (module.controller || function() {}).apply(this, args) || this } var view = function(ctrl) { if (arguments.length > 1) args = args.concat([].slice.call(arguments, 1)) @@ -522,7 +522,7 @@ var m = (function app(window, undefined) { } m.module = function(root, module) { if (!root) throw new Error("Please ensure the DOM element exists before rendering a template into it."); - if (root.controller) return submodule(root, [].slice.call(arguments, 1)) + if (root.view) return submodule(root, [].slice.call(arguments, 1)) var index = roots.indexOf(root); if (index < 0) index = roots.length; diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 3321f8b0..2e620c78 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -102,6 +102,21 @@ function testMithril(mock) { return slot1 == 1 && slot2 == 1 }) + test(function() { + //module should work without controller + mock.requestAnimationFrame.$resolve() + + var root = mock.document.createElement("div") + var slot1, slot2 + var module = { + view: function(ctrl, options) {slot2 = options.a} + } + m.module(root, module, {a: 1}) + + mock.requestAnimationFrame.$resolve() + + return slot2 == 1 + }) test(function() { //arguments should update in component's view mock.requestAnimationFrame.$resolve()