lhorie/mithril.js — pull request #242 by Whoaa512 (2014-09-10 23:38:38+00:00)
Title: M.module return controller

The use case here is that there is a top-level module which contains references to sub-controllers. Currently there is no way to access the sub-controllers if the top-level module is created using `m.module`, and I would like to be able to pass the controllers along to different routes.

Admittedly, exposing the controller instances in this way opens the door for potential misuse by developers, though I think this falls in line with what one would expect m.module to return. I also considered adding a third boolean param to `m.module`, something like `returnController`, to make the return more explicit. However, I'm not entirely sure that it's more useful than just always returning the controller instance.

Please also check the TypeScript definition and doc updates, I think I followed the pattern properly but reassurance is nice. :)

Example of use case below:

```javascript
var app, appCtrl, subMod1, subMod2;

subMod1 = {
controller: function() {},
view: function() {}
};

subMod2 = {
controller: function() {},
view: function() {}
};

app = {
controller: function() {
return {
subMod1: new subMod1.controller,
subMod2: new subMod2.controller
};
},
view: function() {}
};

appCtrl = m.module(document.getElementById('my-content'), app);

m.route(document.getElementById('my-content'), defaultRoute, {
'/sub-mod-1': {
controller: function() {
return appCtrl.subMod1;
},
view: subMod1.view
},
'/sub-mod-2': {
controller: function() {
return appCtrl.subMod2;
},
view: subMod2.view
}
});
```