瀏覽代碼

Commit 12-4: Added nextPath to the route controller and use it to step through posts.

Frederic G. MARAND 9 年之前
父節點
當前提交
57a8df6e70
共有 2 個文件被更改,包括 15 次插入4 次删除
  1. 4 1
      client/templates/posts/posts_list.html
  2. 11 3
      lib/router.js

+ 4 - 1
client/templates/posts/posts_list.html

@@ -3,5 +3,8 @@
     {{#each posts}}
       {{> postItem}}
     {{/each}}
+    {{#if nextPath}}
+      <a class="load-more" href="{{nextPath}}">Load more</a>
+    {{/if}}
   </div>
-</template>
+</template>

+ 11 - 3
lib/router.js

@@ -24,9 +24,17 @@ PostsListController = RouteController.extend({
   waitOn: function () {
     return Meteor.subscribe('posts', this.findOptions());
   },
+  posts: function () {
+    return Posts.find({}, this.findOptions());
+  },
   data: function () {
+    var hasMore = this.posts().count() === this.postsLimit();
+    var nextPath = this.route.path({
+      postsLimit: this.postsLimit() + this.increment
+    });
     return {
-      posts: Posts.find({}, this.findOptions())
+      posts: this.posts(),
+      nextPath: hasMore ? nextPath : null
     };
   }
 });
@@ -59,8 +67,8 @@ 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('/:postLimit?', {
-  name: 'postsList',
+Router.route('/:postsLimit?', {
+  name: 'postsList'
 });
 
 var requireLogin = function () {