mithril-vndb/archive/v0.1.3/mithril.module.html
2014-04-02 15:25:26 -04:00

175 lines
No EOL
8 KiB
HTML

<!doctype html>
<html>
<head>
<title>Mithril</title>
<link href="http://fonts.googleapis.com/css?family=Open+Sans:300italic" rel="stylesheet" />
<link href="lib/prism/prism.css" rel="stylesheet" />
<link href="style.css" rel="stylesheet" />
</head>
<body>
<header>
<nav class="container">
<a href="index.html" class="logo"><span>&#9675;</span> Mithril</a>
<a href="getting-started.html">Guide</a>
<a href="mithril.html">API</a>
<a href="mithril.min.zip">Download</a>
<a href="http://github.com/lhorie/mithril.js" target="_blank">Github</a>
</nav>
</header>
<main>
<section class="content">
<div class="container">
<div class="row">
<div class="col(3,3,12)">
<h2 id="api">API (v0.1.3)</h2>
<h3 id="core">Core</h3>
<ul>
<li><a href="mithril.html">m</a></li>
<li><a href="mithril.prop.html">m.prop</a></li>
<li><a href="mithril.withAttr.html">m.withAttr</a></li>
<li><a href="mithril.module.html">m.module</a></li>
<li><a href="mithril.trust.html">m.trust</a></li>
<li><a href="mithril.render.html">m.render</a></li>
<li><a href="mithril.redraw.html">m.redraw</a></li>
</ul>
<h3 id="routing">Routing</h3>
<ul>
<li><a href="mithril.route.html">m.route</a>
<ul>
<li><a href="mithril.route.html#defining-routes">m.route(rootElement, defaultRoute, routes)</a></li>
<li><a href="mithril.route.html#redirecting">m.route(path)</a></li>
<li><a href="mithril.route.html#mode-abstraction">m.route(element)</a></li>
<li><a href="mithril.route.html#mode">m.route.mode</a></li>
<li><a href="mithril.route.html#param">m.route.param</a></li>
</ul>
</li>
</ul>
<h3 id="data">Data</h3>
<ul>
<li><a href="mithril.request.html">m.request</a></li>
<li><a href="mithril.deferred.html">m.deferred</a></li>
<li><a href="mithril.sync.html">m.sync</a></li>
<li><a href="mithril.computation.html">m.startComputation / m.endComputation</a></li>
</ul>
<h2 id="archive">History</h2>
<ul>
<li><a href="roadmap.html">Roadmap</a></li>
<li><a href="change-log.html">Change log</a></li>
</ul>
</div>
<div class="col(9,9,12)">
<h2 id="m-module">m.module</h2>
<p>A module is an Object with two keys: <code>controller</code> and <code>view</code>. Each of those should point to a Javascript class constructor function.</p>
<p>&#39;m.module&#39; activates a module by instantiating its controller, then instantiating its view and rendering it into a root DOM element.</p>
<p>Conceptually, the easiest way to think of a module is as a logical namespace with which to organize applications. For example, an app might have a dashboard module, a userEditForm module, an autocompleter module, a date formatting module, etc</p>
<p>In the context of single page applications (SPA), a module can often be thought of as the code for a single &quot;page&quot;, i.e. a visual state that is bookmarkable. Module can, however, also represent <em>parts</em> of pages.</p>
<p>Note that a module might have external dependencies and that the dependencies aren&#39;t considered part of the module.</p>
<p>In more complex applications, modules can be nested in a <a href="http://en.wikipedia.org/wiki/Hierarchical_model%E2%80%93view%E2%80%93controller">hierarchical MVC</a> pattern. Nested reusable modules that have views are called <strong>Components</strong>.</p>
<p>Modules and namespaces are often used interchangeably, but namespaces that do not implement the module interface (that is, objects that do not have a property called <code>controller</code> and a property called <code>view</code>) cannot be activated with <code>m.module</code>. For example, a namespace for date formatting utilities could be labeled a &quot;module&quot; (in the generic sense of the word) but it would not contain a view class, and therefore attempting to initialize it via <code>m.module</code> would result in undefined behavior.</p>
<hr>
<h3 id="usage">Usage</h3>
<p>You can make anonymous modules out of existing classes</p>
<pre><code class="lang-javascript">//controller class
var dashboardController = function() {
this.greeting = &quot;Hello&quot;;
};
//view class
var dashboardView = function(ctrl) {
return m(&quot;h1&quot;, ctrl.greeting);
};
//initialize an anonymous module
m.module(document.body, {controller: dashboardController, view: dashboardView});</code></pre>
<p>Typically, however, modules and namespaces are used interchangeably.</p>
<pre><code class="lang-javascript">//`dashboard` is both a namespace and a module
var dashboard = {}
//controller class
dashboard.controller = function() {
this.greeting = &quot;Hello&quot;;
};
//view class
dashboard.view = function(ctrl) {
return m(&quot;h1&quot;, ctrl.greeting);
};
//initialize it
m.module(document.body, dashboard);</code></pre>
<p>The example below shows a component module called <code>user</code> being included in a parent module <code>dashboard</code>.</p>
<pre><code class="lang-javascript">//this is a sample module
var dashboard = {
controller: function() {
this.greeting = &quot;Hello&quot;;
this.user = new user.controller();
},
view: function(controller) {
return [
m(&quot;h1&quot;, controller.greeting),
new user.view(controller.user)
];
}
};
//this module is being included as a component
var user = {
//model
User: function(name) {
this.name = name;
},
//controller
controller: function() {
this.user = new user.User(&quot;John Doe&quot;);
},
//view
view: function(controller) {
return m(&quot;div&quot;, controller.user.name);
}
};
//activate the dashboard module
m.module(document.body, dashboard);</code></pre>
<p>yields:</p>
<pre><code class="lang-markup">&lt;body&gt;
&lt;h1&gt;Hello&lt;/h1&gt;
&lt;div&gt;John Doe&lt;/div&gt;
&lt;/body&gt;</code></pre>
<hr>
<h3 id="signature">Signature</h3>
<p><a href="how-to-read-signatures.html">How to read signatures</a></p>
<pre><code class="lang-clike">void module(DOMElement rootElement, Module module)
where:
Module :: Object { void controller(), void view(Object controllerInstance) }</code></pre>
<ul>
<li><p><strong>DOMElement rootElement</strong></p>
<p>A DOM element which will contain the view&#39;s template.</p>
</li>
<li><p><strong>Module module</strong></p>
<p>A module is supposed to be an Object with two keys: <code>controller</code> and <code>view</code>. Each of those should point to a Javascript class constructor function</p>
<p>The controller class is instantiated immediately upon calling <code>m.module</code>.</p>
<p>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&#39;s constructor.</p>
<p>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.</p>
<p>This &quot;turtles all the way down&quot; approach is the heart of Mithril&#39;s component system.</p>
<p>Components are nothing more than decoupled classes that can be dynamically brought together as required. This permits the swapping of implementations at a routing level (for example, if implementing widgetized versions of existing components) and class dependency hierarchies can be structurally organized to provide uniform interfaces (for unit tests, for example).</p>
</li>
</ul>
</div>
</div>
</div>
</section>
</main>
<footer>
<div class="container">
Released under the <a href="http://opensource.org/licenses/MIT" target="_blank">MIT license</a>
<br />&copy; 2014 Leo Horie
</div>
</footer>
<script src="lib/prism/prism.js"></script>
</body>
</html>