Ver Fonte

Commit 13-5: Added routes for post lists, and pages to display them.

Frederic G. MARAND há 9 anos atrás
pai
commit
182c96ac90
4 ficheiros alterados com 39 adições e 12 exclusões
  1. 6 2
      .jshintrc
  2. 3 1
      client/templates/includes/header.html
  3. 2 2
      client/templates/posts/post_edit.js
  4. 28 7
      lib/router.js

+ 6 - 2
.jshintrc

@@ -128,6 +128,7 @@
       "Random": false,
       "_": false,               // Underscore.js
       "$": false,               // jQuery
+      "Mongo": true,
       "RouteController": false, // iron-router
       "Router": false,          // iron-router
 
@@ -135,11 +136,14 @@
       // - Collections
       "Comments": true,
       "Errors": true,
-      "Mongo": true,
       "Notifications": true,
-      "PostsListController": true,
       "Posts": true,
 
+      // - Controllers
+      "BestPostsController": true,
+      "NewPostsController": true,
+      "PostsListController": true,
+
       // - Helpers
       "createCommentNotification": true,
       "ownsDocument": true,

+ 3 - 1
client/templates/includes/header.html

@@ -7,11 +7,13 @@
         <span class="icon-bar"></span>
         <span class="icon-bar"></span>
       </button>
-      <a class="navbar-brand" href="{{ pathFor 'postsList' }}">Microscope</a>
+      <a class="navbar-brand" href="{{ pathFor 'home' }}">Microscope</a>
     </div>
 
     <div class="collapse navbar-collapse" id="navigation">
       <ul class="nav navbar-nav">
+        <li><a href="{{ pathFor 'newPosts' }}">New</a></li>
+        <li><a href="{{ pathFor 'bestPosts' }}">Best</a></li>
         {{#if currentUser}}
           <li>
             <a href="{{ pathFor 'postSubmit' }}">Submit Post</a>

+ 2 - 2
client/templates/posts/post_edit.js

@@ -47,7 +47,7 @@ Template.postEdit.events({
     if (confirm("Delete this post ?")) {
       var currentPostId = this._id;
       Posts.remove(currentPostId);
-      Router.go('postsList');
+      Router.go('home');
     }
   }
-});
+});

+ 28 - 7
lib/router.js

@@ -17,7 +17,7 @@ PostsListController = RouteController.extend({
   },
   findOptions: function () {
     return {
-      sort: { submitted: -1 },
+      sort: this.sort,
       limit: this.postsLimit()
     };
   },
@@ -29,17 +29,34 @@ PostsListController = RouteController.extend({
   },
   data: function () {
     var hasMore = this.posts().count() === this.postsLimit();
-    var nextPath = this.route.path({
-      postsLimit: this.postsLimit() + this.increment
-    });
     return {
       posts: this.posts(),
       ready: this.postSub.ready,
-      nextPath: hasMore ? nextPath : null
+      nextPath: hasMore ? this.nextPath() : null
     };
   }
 });
 
+NewPostsController = PostsListController.extend({
+  sort: { submitted: -1, _id: -1 },
+  nextPath: function () {
+    return Router.routes.newPosts.path({
+      postsLimit: this.postsLimit() + this.increment
+    });
+  }
+
+});
+
+BestPostsController = PostsListController.extend({
+  sort: { votes: -1, submitted: -1, _id: -1 },
+  nextPath: function () {
+    return Router.routes.bestPosts.path({
+      postsLimit: this.postsLimit() + this.increment
+    });
+  }
+
+});
+
 Router.route('/posts/:_id', {
   name: 'postPage',
   waitOn: function () {
@@ -71,10 +88,14 @@ Router.route('/submit', {
 
 // C'est un nom de route, pas un nom de template. Mais IR le prend comme nom de
 // template par défaut.
-Router.route('/:postsLimit?', {
-  name: 'postsList'
+Router.route('/', {
+  name: 'home',
+  controller: 'NewPostsController',
 });
 
+Router.route('/new/:postsLimit?', { name: 'newPosts' });
+Router.route('/best/:postsLimit?', { name: 'bestPosts' });
+
 var requireLogin = function () {
   if (!Meteor.user()) {
     if (Meteor.loggingIn()) {