defer calling of configs until DOM exists

This commit is contained in:
Leo Horie 2014-06-05 21:54:51 -04:00
parent 2d8db79cef
commit e82ac2ef71
3 changed files with 43 additions and 1 deletions

View file

@ -32,6 +32,7 @@ Mithril = m = new function app(window) {
} }
return cell return cell
} }
var configs = []
function build(parentElement, parentTag, parentCache, parentIndex, data, cached, shouldReattach, index, editable, namespace) { function build(parentElement, parentTag, parentCache, parentIndex, data, cached, shouldReattach, index, editable, namespace) {
if (data === null || data === undefined) data = "" if (data === null || data === undefined) data = ""
if (data.subtree === "retain") return if (data.subtree === "retain") return
@ -96,7 +97,9 @@ Mithril = m = new function app(window) {
cached.nodes.intact = true cached.nodes.intact = true
if (shouldReattach === true) parentElement.insertBefore(node, parentElement.childNodes[index] || null) 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 { else {
var node var node
@ -224,6 +227,8 @@ Mithril = m = new function app(window) {
var id = index < 0 ? nodeCache.push(root) - 1 : index var id = index < 0 ? nodeCache.push(root) - 1 : index
var node = root == window.document || root == window.document.documentElement ? documentNode : root 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) 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) { m.trust = function(value) {

View file

@ -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') var dummyEl = document.getElementById('dummy')
test('Mithril accessible as window.m', function() { test('Mithril accessible as window.m', function() {

View file

@ -479,6 +479,14 @@ function testMithril(mock) {
m.render(root, [node, node]); m.render(root, [node, node]);
return success; 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 //end m.render
//m.redraw //m.redraw