diff --git a/mithril.js b/mithril.js index 5311a10e..e0281a9e 100644 --- a/mithril.js +++ b/mithril.js @@ -32,6 +32,7 @@ Mithril = m = new function app(window) { } return cell } + var configs = [] function build(parentElement, parentTag, parentCache, parentIndex, data, cached, shouldReattach, index, editable, namespace) { if (data === null || data === undefined) data = "" if (data.subtree === "retain") return @@ -96,7 +97,9 @@ Mithril = m = new function app(window) { cached.nodes.intact = true if (shouldReattach === true) parentElement.insertBefore(node, parentElement.childNodes[index] || null) } - if (type.call(data.attrs["config"]) == "[object Function]") data.attrs["config"](node, !isNew, cached.configContext = cached.configContext || {}) + if (type.call(data.attrs["config"]) == "[object Function]") { + configs.push(data.attrs["config"].bind(window, node, !isNew, cached.configContext = cached.configContext || {})) + } } else { var node @@ -224,6 +227,8 @@ Mithril = m = new function app(window) { var id = index < 0 ? nodeCache.push(root) - 1 : index var node = root == window.document || root == window.document.documentElement ? documentNode : root cellCache[id] = build(node, null, undefined, undefined, cell, cellCache[id], false, 0, null, undefined) + for (var i = 0; i < configs.length; i++) configs[i]() + configs.length = 0 } m.trust = function(value) { diff --git a/tests/e2e/tests.js b/tests/e2e/tests.js index 0d60cf7f..0b4cf22c 100644 --- a/tests/e2e/tests.js +++ b/tests/e2e/tests.js @@ -1,3 +1,32 @@ +//qunit doesn't support Function.prototype.bind... +if (!Function.prototype.bind) { + Function.prototype.bind = function (oThis) { + if (typeof this !== "function") { + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function () {}, + fBound = function () { + return fToBind.apply(this instanceof fNOP && oThis + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + fNOP.prototype = this.prototype; + fBound.prototype = new fNOP(); + + return fBound; + }; +} + + + +//tests var dummyEl = document.getElementById('dummy') test('Mithril accessible as window.m', function() { diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index b369d34e..3ce7a667 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -479,6 +479,14 @@ function testMithril(mock) { m.render(root, [node, node]); return success; }) + test(function() { + var root = mock.document.createElement("div") + var parent + m.render(root, m("div", m("a", { + config: function(el) {parent = el.parentNode.parentNode} + }))); + return parent === root + }) //end m.render //m.redraw