Merge pull request #242 from Whoaa512/m.module-return-controller

M.module return controller
This commit is contained in:
Leo Horie 2014-09-14 00:59:44 -04:00
commit 7d718a9e76
4 changed files with 16 additions and 14 deletions

View file

@ -155,7 +155,7 @@ module1.controller = function() {
[How to read signatures](how-to-read-signatures.md) [How to read signatures](how-to-read-signatures.md)
```clike ```clike
void module(DOMElement rootElement, Module module) Object module(DOMElement rootElement, Module module)
where: where:
Module :: Object { Controller, void view(Object controllerInstance) } Module :: Object { Controller, void view(Object controllerInstance) }
@ -171,7 +171,7 @@ where:
A module is supposed to be an Object with two keys: `controller` and `view`. Each of those should point to a Javascript class constructor function A module is supposed to be an Object with two keys: `controller` and `view`. Each of those should point to a Javascript class constructor function
The controller class is instantiated immediately upon calling `m.module`. The controller class is instantiated immediately and a reference is returned upon calling `m.module`.
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. 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.

2
mithril.d.ts vendored
View file

@ -5,7 +5,7 @@ interface MithrilStatic {
(selector: string, children?: any): MithrilVirtualElement; (selector: string, children?: any): MithrilVirtualElement;
prop(value?: any): (value?: any) => any; prop(value?: any): (value?: any) => any;
withAttr(property: string, callback: (value: any) => void): (e: Event) => any; withAttr(property: string, callback: (value: any) => void): (e: Event) => any;
module(rootElement: Element, module: MithrilModule): void; module(rootElement: Element, module: MithrilModule): Object;
trust(html: string): String; trust(html: string): String;
render(rootElement: Element, children?: any): void; render(rootElement: Element, children?: any): void;
render(rootElement: HTMLDocument, children?: any): void; render(rootElement: HTMLDocument, children?: any): void;

View file

@ -457,6 +457,7 @@ Mithril = m = new function app(window, undefined) {
modules[index] = module modules[index] = module
controllers[index] = new module.controller controllers[index] = new module.controller
m.endComputation() m.endComputation()
return controllers[index]
} }
} }
m.redraw = function(force) { m.redraw = function(force) {

View file

@ -34,20 +34,21 @@ function testMithril(mock) {
mock.requestAnimationFrame.$resolve() mock.requestAnimationFrame.$resolve()
var root1 = mock.document.createElement("div") var root1 = mock.document.createElement("div")
m.module(root1, { var mod1 = m.module(root1, {
controller: function() {this.value = "test1"}, controller: function() {this.value = "test1"},
view: function(ctrl) {return ctrl.value} view: function(ctrl) {return ctrl.value}
}) })
var root2 = mock.document.createElement("div") var root2 = mock.document.createElement("div")
m.module(root2, { var mod2 = m.module(root2, {
controller: function() {this.value = "test2"}, controller: function() {this.value = "test2"},
view: function(ctrl) {return ctrl.value} view: function(ctrl) {return ctrl.value}
}) })
mock.requestAnimationFrame.$resolve() mock.requestAnimationFrame.$resolve()
return root1.childNodes[0].nodeValue === "test1" && root2.childNodes[0].nodeValue === "test2" return (root1.childNodes[0].nodeValue === "test1" && root2.childNodes[0].nodeValue === "test2")
&& (mod1.value && mod1.value === "test1") && (mod2.value && mod2.value === "test2")
}) })
//m.withAttr //m.withAttr