Permalink
Browse files

Denormalized the number of comments into the post.

chapter10-5
  • Loading branch information...
tmeasday committed Apr 10, 2013
1 parent 944cb07 commit 767f0c51dc55f93099e1cfd39e48b57849e8f2f8
Showing with 11 additions and 7 deletions.
  1. +0 −3 client/templates/posts/post_item.js
  2. +3 −0 lib/collections/comments.js
  3. +2 −1 lib/collections/posts.js
  4. +6 −3 server/fixtures.js
@@ -6,8 +6,5 @@ Template.postItem.helpers({
var a = document.createElement('a');
a.href = this.url;
return a.hostname;
- },
- commentsCount: function() {
- return Comments.find({postId: this._id}).count();
}
});
@@ -20,6 +20,9 @@ Meteor.methods({
submitted: new Date()
});
+ // update the post with the number of comments
+ Posts.update(comment.postId, {$inc: {commentsCount: 1}});
+
return Comments.insert(comment);
}
});
View
@@ -55,7 +55,8 @@ Meteor.methods({
var post = _.extend(postAttributes, {
userId: user._id,
author: user.username,
- submitted: new Date()
+ submitted: new Date(),
+ commentsCount: 0
});
var postId = Posts.insert(post);
View
@@ -17,7 +17,8 @@ if (Posts.find().count() === 0) {
userId: sacha._id,
author: sacha.profile.name,
url: 'http://sachagreif.com/introducing-telescope/',
- submitted: new Date(now - 7 * 3600 * 1000)
+ submitted: new Date(now - 7 * 3600 * 1000),
+ commentsCount: 2
});
Comments.insert({
@@ -41,14 +42,16 @@ if (Posts.find().count() === 0) {
userId: tom._id,
author: tom.profile.name,
url: 'http://meteor.com',
- submitted: new Date(now - 10 * 3600 * 1000)
+ submitted: new Date(now - 10 * 3600 * 1000),
+ commentsCount: 0
});
Posts.insert({
title: 'The Meteor Book',
userId: tom._id,
author: tom.profile.name,
url: 'http://themeteorbook.com',
- submitted: new Date(now - 12 * 3600 * 1000)
+ submitted: new Date(now - 12 * 3600 * 1000),
+ commentsCount: 0
});
}

0 comments on commit 767f0c5

Please sign in to comment.