#420 allow unloading of modules
This commit is contained in:
parent
fed82b2135
commit
a04d67500e
3 changed files with 34 additions and 4 deletions
|
|
@ -118,6 +118,12 @@ module1.controller = function() {
|
|||
}
|
||||
```
|
||||
|
||||
To unload a module without loading another module, you can simply call `m.module` without a module parameter:
|
||||
|
||||
```javascript
|
||||
m.module(rootElement, null);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Signature
|
||||
|
|
@ -145,7 +151,7 @@ where:
|
|||
|
||||
Once the controller code finishes executing (and this may include waiting for AJAX requests to complete), the view class is instantiated, and the instance of the controller is passed as an argument to the view's constructor.
|
||||
|
||||
Note that controllers can manually instantiate child controllers (since they are simply Javascript constructors), and likewise, views can instantiate child views and manually pass the child controller instances down the the child view constructors. You should avoid instantiating controllers from views, since views can be rendered many times across the lifecycle of a page, and a redraw might wipe out sub-controller data, if it houses any.
|
||||
Note that controllers can manually instantiate child controllers (since they are simply Javascript constructors), and likewise, views can call child views and manually pass the child controller instances down the the child view constructors. You should avoid instantiating controllers from views, since views can be rendered many times across the lifecycle of a page, and a redraw might wipe out sub-controller data, if it houses any.
|
||||
|
||||
This "[turtles all the way down](https://en.wikipedia.org/wiki/Turtles_all_the_way_down)" approach is the heart of Mithril's component system.
|
||||
|
||||
|
|
|
|||
|
|
@ -508,8 +508,8 @@ var m = (function app(window, undefined) {
|
|||
m.redraw.strategy("all");
|
||||
m.startComputation();
|
||||
roots[index] = root;
|
||||
var currentModule = topModule = module;
|
||||
var controller = new module.controller;
|
||||
var currentModule = topModule = module = module || {};
|
||||
var controller = new (module.controller || function() {});
|
||||
//controllers may call m.module recursively (via m.route redirects, for example)
|
||||
//this conditional ensures only the last recursive m.module call is applied
|
||||
if (currentModule === topModule) {
|
||||
|
|
@ -537,11 +537,12 @@ var m = (function app(window, undefined) {
|
|||
}
|
||||
};
|
||||
m.redraw.strategy = m.prop();
|
||||
var blank = function() {return ""}
|
||||
function redraw() {
|
||||
var forceRedraw = m.redraw.strategy() === "all";
|
||||
for (var i = 0, root; root = roots[i]; i++) {
|
||||
if (controllers[i]) {
|
||||
m.render(root, modules[i].view(controllers[i]), forceRedraw)
|
||||
m.render(root, (modules[i].view || blank)(controllers[i]), forceRedraw)
|
||||
}
|
||||
}
|
||||
//after rendering within a routed context, we need to scroll back to the top, and fetch the document title for history.pushState
|
||||
|
|
|
|||
|
|
@ -55,6 +55,29 @@ function testMithril(mock) {
|
|||
return (root1.childNodes[0].nodeValue === "test1" && root2.childNodes[0].nodeValue === "test2")
|
||||
&& (mod1.value && mod1.value === "test1") && (mod2.value && mod2.value === "test2")
|
||||
})
|
||||
test(function() {
|
||||
mock.requestAnimationFrame.$resolve()
|
||||
|
||||
var root = mock.document.createElement("div")
|
||||
var unloaded = false
|
||||
var mod = m.module(root, {
|
||||
controller: function() {
|
||||
this.value = "test1"
|
||||
this.onunload = function() {
|
||||
unloaded = true
|
||||
}
|
||||
},
|
||||
view: function(ctrl) {return ctrl.value}
|
||||
})
|
||||
|
||||
mock.requestAnimationFrame.$resolve()
|
||||
|
||||
m.module(root, null)
|
||||
|
||||
mock.requestAnimationFrame.$resolve()
|
||||
|
||||
return unloaded
|
||||
})
|
||||
|
||||
//m.withAttr
|
||||
test(function() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue