From ad8b58107700e62382ef770cee4171fe19877ea9 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sat, 24 Jan 2015 18:09:28 -0500 Subject: [PATCH] proof of concept for components --- mithril.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/mithril.js b/mithril.js index bfdcc2f6..2f9fd8e9 100644 --- a/mithril.js +++ b/mithril.js @@ -237,9 +237,10 @@ var m = (function app(window, undefined) { var dataAttrKeys = Object.keys(data.attrs) var hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0) //if an element is different enough from the one in cache, recreate it - if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id) { + if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id || data.attrs.key != cached.attrs.key) { if (cached.nodes.length) clear(cached.nodes); if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload() + if (cached.controller && typeof cached.controller.onunload === FUNCTION) cached.controller.onunload({preventDefault: function() {}}) } if (type.call(data.tag) != STRING) return; @@ -247,7 +248,15 @@ var m = (function app(window, undefined) { if (data.attrs.xmlns) namespace = data.attrs.xmlns; else if (data.tag === "svg") namespace = "http://www.w3.org/2000/svg"; else if (data.tag === "math") namespace = "http://www.w3.org/1998/Math/MathML"; + if (isNew) { + var module = m.tags[data.tag] + if (module) { + var constructor = module.controller || m.prop() + var controller = new constructor(data) + data = module.view(controller) + } + if (data.attrs.is) node = namespace === undefined ? $document.createElement(data.tag, data.attrs.is) : $document.createElementNS(namespace, data.tag, data.attrs.is); else node = namespace === undefined ? $document.createElement(data.tag) : $document.createElementNS(namespace, data.tag); cached = { @@ -259,6 +268,9 @@ var m = (function app(window, undefined) { data.children, nodes: [node] }; + + if (module) cached.controller = controller + if (cached.children && !cached.children.nodes) cached.children.nodes = []; //edge case: setting value on