Browse Source

Chapter 5: routing.

Frederic G. MARAND 9 years ago
parent
commit
b7df5b89e1

+ 1 - 0
.meteor/packages

@@ -8,3 +8,4 @@ meteor-platform
 iron:router
 twbs:bootstrap
 underscore
+sacha:spin

+ 1 - 0
.meteor/versions

@@ -42,6 +42,7 @@ reactive-var@1.0.5
 reload@1.1.3
 retry@1.0.3
 routepolicy@1.0.5
+sacha:spin@2.3.1
 session@1.1.0
 spacebars@1.0.6
 spacebars-compiler@1.0.6

+ 0 - 1
client/main.js

@@ -1 +0,0 @@
-Meteor.subscribe('posts');

+ 1 - 1
client/templates/application/layout.html

@@ -2,7 +2,7 @@
   <div class="container">
     <header class="navbar navbar-default" role="navigation">
       <div class="navbar-header">
-        <a class="navbar-brand" href="/">Microscope</a>
+        <a class="navbar-brand" href="{{pathFor 'postsList'}}">Microscope</a>
       </div>
     </header>
 

+ 3 - 0
client/templates/includes/loading.html

@@ -0,0 +1,3 @@
+<template name="loading">
+  {{> spinner}}
+</template>

+ 5 - 0
client/templates/post_page.html

@@ -0,0 +1,5 @@
+<template name="postPage">
+  <div class="post-page page">
+    {{> postItem}}
+  </div>
+</template>

+ 6 - 0
client/templates/posts/not_found.html

@@ -0,0 +1,6 @@
+<template name="notFound">
+  <div class="not-found page jumbotron">
+    <h2>404</h2>
+    <p>Sorry, we couldn't find a page at this address.</p>
+  </div>
+</template>

+ 7 - 0
client/templates/posts/post_item.html

@@ -3,5 +3,12 @@
     <div class="post-content">
       <h3><a href="{{url}}">{{title}}</a><span>{{domain}}</span></h3>
     </div>
+    {{! La route postPage nécessite un _id, donc IR va le chercher dans le
+      data context, qui se trouve à ce stade être le document concerné, qui
+      possède bien un _id.
+      Si ce n'avait pas été le cas, on aurait pu le spécifier:
+      pathFor 'postPage' someOtherPostId
+    }}
+    <a href="{{pathFor 'postPage'}}" class="discuss btn btn-default">Discuss</a>
   </div>
 </template>

+ 24 - 2
lib/router.js

@@ -8,7 +8,29 @@
  */
 
 Router.configure({
-    layoutTemplate: "layout"
+  layoutTemplate: "layout",
+  loadingTemplate: "loading",
+  notFoundTemplate: "notFound",
+  waitOn: function () {
+    return Meteor.subscribe('posts');
+  }
 });
 
-Router.route('/', { name: 'postsList' });
+// C'est un nom de route, pas un nom de template. Mais IR le prend comme nom de template par défaut.
+Router.route('/', {
+  name: 'postsList'
+});
+
+Router.route('/posts/:_id', {
+  name: 'postPage',
+  data: function () {
+    // "this" is the matched route.
+    return Posts.findOne(this.params._id);
+  }
+});
+
+// Faire une 404 si la page matche la route postPage, mais pas son argument.
+// Déclenché pour toute valeur "falsy" (null, false, undefined, empty).
+Router.onBeforeAction('dataNotFound', {
+  only: 'postPage'
+});