From 10f291d64a81480f06f317dd2db36fe086025a17 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Fri, 30 Jan 2015 22:41:35 -0500 Subject: [PATCH] components API: take 2 --- mithril.js | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/mithril.js b/mithril.js index 31fc01f8..cac61696 100644 --- a/mithril.js +++ b/mithril.js @@ -231,19 +231,13 @@ var m = (function app(window, undefined) { } } else if (data != null && dataType === OBJECT) { - var module = m.tags[data.tag], controller = cached.controller - if (module) { - if (!controller) { - var constructor = module.controller || m.prop() - for (var prop in constructor.prototype) data[prop] = constructor.prototype[prop] - controller = constructor.call(data, data) || data - if (!controller.attrs) controller.attrs = {} - } - controller.tag = data.tag - for (var attr in data.attrs) controller.attrs[attr] = data.attrs[attr] - controller.children = data.children + if (data.controller) { + var module = data + var controller = cached.controller || new (module.controller || function() {}) + if (cached.controller && typeof cached.controller.onupdate === FUNCTION) controller.onupdate.apply(controller, args) + controller.args = module.controller.$$args data = module.view(controller) - if (!data.tag) throw new Error(module.view.toString() + "\n\nThis template must return a virtual element, not an array, string, etc."); + if (!data.tag) throw new Error(module.view.toString() + "\n\nThis template must return a virtual element, not an array, string, etc.") } if (!data.attrs) data.attrs = {}; if (!cached.attrs) cached.attrs = {}; @@ -275,7 +269,7 @@ var m = (function app(window, undefined) { data.children, nodes: [node] }; - if (module) cached.controller = controller + if (controller) cached.controller = controller if (cached.children && !cached.children.nodes) cached.children.nodes = []; //edge case: setting value on