add ability to abort from controller::onunload

This commit is contained in:
Leo Horie 2014-06-18 07:44:51 -04:00
parent 81f3367be9
commit b486e53b4b
3 changed files with 31 additions and 7 deletions

View file

@ -297,14 +297,22 @@ Mithril = m = new function app(window) {
var roots = [], modules = [], controllers = [], now = 0, lastRedraw = 0, lastRedrawId = 0, computePostRedrawHook = null
m.module = function(root, module) {
m.startComputation()
var index = roots.indexOf(root)
if (index < 0) index = roots.length
roots[index] = root
modules[index] = module
if (controllers[index] && typeof controllers[index].onunload == "function") controllers[index].onunload()
controllers[index] = new module.controller
m.endComputation()
var isPrevented = false
if (controllers[index] && typeof controllers[index].onunload == "function") {
var event = {
preventDefault: function() {isPrevented = true}
}
controllers[index].onunload(event)
}
if (!isPrevented) {
m.startComputation()
roots[index] = root
modules[index] = module
controllers[index] = new module.controller
m.endComputation()
}
}
m.redraw = function() {
now = window.performance && window.performance.now ? window.performance.now() : new window.Date().getTime()