allow m.redraw(true) to force-render component

This commit is contained in:
Leo Horie 2015-05-05 21:16:29 -04:00
parent cd403225a2
commit 2d5d042131
2 changed files with 30 additions and 3 deletions

View file

@ -245,7 +245,7 @@ var m = (function app(window, undefined) {
var controllerIndex = m.redraw.strategy() == "diff" && cached.views ? cached.views.indexOf(view) : -1
var controller = controllerIndex > -1 ? cached.controllers[controllerIndex] : new (data.controller || noop)
var key = data && data.attrs && data.attrs.key
data = pendingRequests == 0 || (cached && cached.controllers && cached.controllers.indexOf(controller) > -1) ? data.view(controller) : {tag: "placeholder"}
data = (pendingRequests == 0 || forcing) || (cached && cached.controllers && cached.controllers.indexOf(controller) > -1) ? data.view(controller) : {tag: "placeholder"}
if (data.subtree === "retain") return cached;
if (key) {
if (!data.attrs) data.attrs = {}
@ -606,10 +606,11 @@ var m = (function app(window, undefined) {
return controllers[index]
}
};
var redrawing = false
var redrawing = false, forcing = false
m.redraw = function(force) {
if (redrawing) return
redrawing = true
if (force) forcing = true
//lastRedrawId is a positive number if a second redraw is requested before the next animation frame
//lastRedrawID is null if it's the first redraw and not an event handler
if (lastRedrawId && force !== true) {
@ -624,7 +625,7 @@ var m = (function app(window, undefined) {
redraw();
lastRedrawId = $requestAnimationFrame(function() {lastRedrawId = null}, FRAME_BUDGET)
}
redrawing = false
redrawing = forcing = false
};
m.redraw.strategy = m.prop();
function redraw() {

View file

@ -1339,6 +1339,32 @@ function testMithril(mock) {
return initialized === false
})
test(function() {
var root = mock.document.createElement("div")
var el
var FooPage = {
view: function(ctrl) {
return m('div', [
m('button', {onclick: function() {
ctrl.bar = true;
m.redraw(true);
el = root.childNodes[0].childNodes[1]
}}, 'click me'),
ctrl.bar ? m.component(BarComponent) : ''
]);
}
};
var BarComponent = {
view: function() {
return m('#bar', 'test');
}
};
m.mount(root, FooPage);
root.childNodes[0].childNodes[0].onclick({})
return el.id == "bar"
})
//m.withAttr
test(function() {