editorial

This commit is contained in:
Leo Horie 2014-05-27 08:51:34 -04:00
parent d9243f4998
commit 0550079bfc
19 changed files with 93 additions and 91 deletions

View file

@ -45,7 +45,7 @@
</div>
<div class="col(9,9,12)">
<h2 id="routing">Routing</h2>
<p>Routing is a system that allows creating Single-Page-Applications (SPA), i.e. applications that can go from a page to another without causing a full browser refresh.</p>
<p>Routing is a system that allows creating Single-Page-Applications (SPA), i.e. applications that can go from one page to another without causing a full browser refresh.</p>
<p>It enables seamless navigability while preserving the ability to bookmark each page individually, and the ability to navigate the application via the browser&#39;s history mechanism.</p>
<p>Mithril provides utilities to handle three different aspect of routing:</p>
<ul>
@ -56,14 +56,14 @@
<hr>
<h3 id="defining-routes">Defining routes</h3>
<p>To define a list of routes, you need to specify a host DOM element, a default route and a key-value map of possible routes and respective <a href="mithril.module.html">modules</a> to be rendered.</p>
<p>The example below defines 3 routes, to be rendered in <code>&lt;body&gt;</code>. <code>home</code>, <code>login</code> and <code>dashboard</code> are modules. We&#39;ll see how to define a module in a bit.</p>
<p>The example below defines three routes, to be rendered in <code>&lt;body&gt;</code>. <code>home</code>, <code>login</code> and <code>dashboard</code> are modules. We&#39;ll see how to define a module in a bit.</p>
<pre><code class="lang-javascript">m.route(document.body, &quot;/&quot;, {
&quot;/&quot;: home,
&quot;/login&quot;: login,
&quot;/dashboard&quot;: dashboard,
});</code></pre>
<p>Routes can take arguments, by prefixing words with a colon <code>:</code></p>
<p>The example below shows a route that takes an <code>userID</code> parameter</p>
<p>Routes can take arguments, by prefixing words with a colon <code>:</code>.</p>
<p>The example below shows a route that takes a <code>userID</code> parameter.</p>
<pre><code class="lang-javascript">//a sample module
var dashboard = {
controller: function() {
@ -83,10 +83,10 @@ m.route(document.body, &quot;/dashboard/johndoe&quot;, {
m.route.mode = &quot;hash&quot;;</code></pre>
<p>This redirects to the URL <code>http://server/#/dashboard/johndoe</code> and yields:</p>
<pre><code class="lang-markup">&lt;body&gt;johndoe&lt;/body&gt;</code></pre>
<p>Above, <code>dashboard</code> is a module. It contains a <code>controller</code> and a <code>view</code> properties. When the URL matches a route, the respective module&#39;s controller is instantiated and passed as a parameter to the view.</p>
<p>In this case, since there&#39;s only route, the app redirects to the default route <code>&quot;/dashboard/johndoe&quot;</code> and, under the hood, it calls <code>m.module(document.body, dashboard)</code>.</p>
<p>The string <code>johndoe</code> is bound to the <code>:userID</code> parameter, which can be retrived programmatically in the controller via <code>m.route.param(&quot;userID&quot;)</code>.</p>
<p>The <code>m.route.mode</code> property defines which URL portion is used to implement the routing mechanism. Its value can be set to either &quot;search&quot;, &quot;hash&quot; or &quot;pathname&quot;. The default value is &quot;search&quot;</p>
<p>Above, <code>dashboard</code> is a module. It contains <code>controller</code> and <code>view</code> properties. When the URL matches a route, the respective module&#39;s controller is instantiated and passed as a parameter to the view.</p>
<p>In this case, since there&#39;s only one route, the app redirects to the default route <code>&quot;/dashboard/johndoe&quot;</code> and, under the hood, it calls <code>m.module(document.body, dashboard)</code>.</p>
<p>The string <code>johndoe</code> is bound to the <code>:userID</code> parameter, which can be retrieved programmatically in the controller via <code>m.route.param(&quot;userID&quot;)</code>.</p>
<p>The <code>m.route.mode</code> property defines which URL portion is used to implement the routing mechanism. Its value can be set to either &quot;search&quot;, &quot;hash&quot; or &quot;pathname&quot;. The default value is &quot;search&quot;.</p>
<ul>
<li><p><code>search</code> mode uses the querystring. This allows named anchors (i.e. <code>&lt;a href=&quot;#top&quot;&gt;Back to top&lt;/a&gt;</code>, <code>&lt;a name=&quot;top&quot;&gt;&lt;/a&gt;</code>) to work on the page, but routing changes causes page refreshes in IE8, due to its lack of support for <code>history.pushState</code>.</p>
<p>Example URL: <code>http://server/?/path/to/page</code></p>
@ -94,14 +94,14 @@ m.route.mode = &quot;hash&quot;;</code></pre>
<li><p><code>hash</code> mode uses the hash. It&#39;s the only mode in which routing changes do not cause page refreshes in any browser. However, this mode does not support named anchors and browser history lists.</p>
<p>Example URL: <code>http://server/#/path/to/page</code></p>
</li>
<li><p><code>pathname</code> mode allows routing URLs that contains no special characters, however this mode requires server-side setup in order to support bookmarking and page refreshes. It also causes page refreshes in IE8.</p>
<li><p><code>pathname</code> mode allows routing URLs that contain no special characters, however this mode requires server-side setup in order to support bookmarking and page refreshes. It also causes page refreshes in IE8.</p>
<p>Example URL: <code>http://server/path/to/page</code></p>
<p>The simplest server-side setup possible to support pathname mode is to serve the same content regardless of what URL is requested. In Apache, this URL rewriting can be achieved using ModRewrite.</p>
<p>The simplest server-side setup possible to support pathname mode is to serve the same content regardless of what URL is requested. In Apache, this URL rewriting can be achieved using <a href="https://httpd.apache.org/docs/current/mod/mod_rewrite.html">mod_rewrite</a>.</p>
</li>
</ul>
<hr>
<h3 id="redirecting">Redirecting</h3>
<p>You can programmatically redirec to another page. Given the example in the &quot;Defining Routes&quot; section:</p>
<p>You can programmatically redirect to another page. Given the example in the &quot;Defining Routes&quot; section:</p>
<pre><code class="lang-javascript">m.route(&quot;/dashboard/marysue&quot;);</code></pre>
<p>redirects to <code>http://server/#/dashboard/marysue</code></p>
<hr>