Merge pull request #119 from pistolero/config_context
Added context for config handlers
This commit is contained in:
commit
f7240e0ce5
3 changed files with 49 additions and 2 deletions
|
|
@ -96,7 +96,7 @@ 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)
|
if (type.call(data.attrs["config"]) == "[object Function]") data.attrs["config"](node, !isNew, cached.config_context=cached.config_context || {})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var node
|
var node
|
||||||
|
|
|
||||||
|
|
@ -45,3 +45,20 @@ test('issue99 regression', function() {
|
||||||
m.render(dummyEl, view2);
|
m.render(dummyEl, view2);
|
||||||
equal(dummyEl.innerHTML, '<div><span>0</span></div>', 'view2 should be rendered correctly');
|
equal(dummyEl.innerHTML, '<div><span>0</span></div>', 'view2 should be rendered correctly');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('config handler context', function() {
|
||||||
|
expect(3);
|
||||||
|
var view = m('div', {config: function(evt, isInitialized, context){
|
||||||
|
equal(context instanceof Object, true);
|
||||||
|
context.data = 1;
|
||||||
|
}})
|
||||||
|
m.render(dummyEl, view);
|
||||||
|
|
||||||
|
var view = m('div', {config: function(evt, isInitialized, context){
|
||||||
|
equal(context instanceof Object, true);
|
||||||
|
equal(context.data, 1);
|
||||||
|
}})
|
||||||
|
m.render(dummyEl, view);
|
||||||
|
|
||||||
|
})
|
||||||
|
|
|
||||||
|
|
@ -998,9 +998,39 @@ function testMithril(mock) {
|
||||||
test(function() {
|
test(function() {
|
||||||
return m.deps.factory.toString().indexOf("console") < 0
|
return m.deps.factory.toString().indexOf("console") < 0
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// config context
|
||||||
|
test(function() {
|
||||||
|
var root = mock.document.createElement("div")
|
||||||
|
|
||||||
|
var success = false;
|
||||||
|
m.render(root, m("div", {config: function(elem, isInitialized, ctx) {ctx.data=1}}));
|
||||||
|
m.render(root, m("div", {config: function(elem, isInitialized, ctx) {success = ctx.data===1}}));
|
||||||
|
return success;
|
||||||
|
})
|
||||||
|
|
||||||
|
// more complex config context
|
||||||
|
test(function() {
|
||||||
|
var root = mock.document.createElement("div")
|
||||||
|
|
||||||
|
var idx = 0;
|
||||||
|
var success = true;
|
||||||
|
var statefulConfig = function(elem, isInitialized, ctx) {ctx.data=idx++}
|
||||||
|
var node = m("div", {config: statefulConfig});
|
||||||
|
m.render(root, [node, node]);
|
||||||
|
|
||||||
|
idx = 0;
|
||||||
|
var checkConfig = function(elem, isInitialized, ctx) {
|
||||||
|
success = success && (ctx.data === idx++)
|
||||||
|
}
|
||||||
|
node = m("div", {config: checkConfig});
|
||||||
|
m.render(root, [node, node]);
|
||||||
|
return success;
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//mocks
|
//mocks
|
||||||
testMithril(mock.window)
|
testMithril(mock.window)
|
||||||
|
|
||||||
test.print(console.log)
|
test.print(console.log)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue