Voting in Discover Meteor Vietnamese Version
-
Upload
namcuahiem -
Category
Documents
-
view
227 -
download
0
Transcript of Voting in Discover Meteor Vietnamese Version
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
1/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
2/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
3/24
// Fixture data
if(Posts.find().count() ===0) {
varnow = newDate().getTime();
// create two usersvartomId =Meteor.users.insert({
profile: {name:'Tom Coleman'}
});
vartom =Meteor.users.findOne(tomId);
varsachaId =Meteor.users.insert({
profile: {name:'Sacha Greif'}
});
varsacha =Meteor.users.findOne(sachaId);
vartelescopeId =Posts.insert({
title:'Introducing Telescope',
userId: sacha._id,
author: sacha.profile.name,
url:'http://sachagreif.com/introducing-telescope/',
submitted: newDate(now -7 *3600 *1000),
commentsCount:2,
upvoters: [],
votes:0
});
Comments.insert({
postId: telescopeId,
userId: tom._id,
author: tom.profile.name,
submitted: newDate(now -5 *3600 *1000),
body:'Interesting project Sacha, can I get involved?'});
Comments.insert({
postId: telescopeId,
userId: sacha._id,
author: sacha.profile.name,
submitted: newDate(now -3 *3600 *1000),
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
4 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
4/24
body:'You sure can Tom!'
});
Posts.insert({
title:'Meteor',
userId: tom._id,
author: tom.profile.name,url:'http://meteor.com',
submitted: newDate(now -10 *3600 *1000),
commentsCount:0,
upvoters: [],
votes:0
});
Posts.insert({
title:'The Meteor Book',
userId: tom._id,
author: tom.profile.name,
url:'http://themeteorbook.com',
submitted: newDate(now -12 *3600 *1000),
commentsCount:0,
upvoters: [],
votes:0
});
for(vari =0; i
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
5/24
meteor reset
//...
varpostWithSameLink =Posts.findOne({url: postAttributes.url});
if(postWithSameLink) {
return{
postExists: true,
_id: postWithSameLink._id
}
}
varuser =Meteor.user();
varpost =_.extend(postAttributes, {
userId: user._id,
author: user.username,
submitted: newDate(),
commentsCount:0,upvoters: [],
votes:0
});
varpostId =Posts.insert(post);
return{
_id: postId
};
//...
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
4 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
6/24
{{title}}{{domain}}
{{votes}} Votes,
submitted by {{author}},
{{commentsCount}} comments
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
7/24
//...
Template.postItem.events({
'click .upvote': function(e) {
e.preventDefault();
Meteor.call('upvote', this._id);}
});
lib/collections/posts.js
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
4 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
8/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
9/24
$inc
disabled
//...
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
4 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
10/24
Template.postItem.helpers({
ownPost: function() {
//...
},
domain: function() {//...
},
upvotedClass: function() {
varuserId =Meteor.userId();
if(userId && !_.include(this.upvoters, userId)) {
return'btn-primary upvotable';
} else{
return'disabled';
}
}
});
Template.postItem.events({
'click .upvotable': function(e) {
e.preventDefault();
Meteor.call('upvote', this._id);
}
});
.upvote .upvotable
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
11/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
12/24
//...
{{pluralize votes "Vote"}},
submitted by {{author}},
{{pluralize commentsCount "comment"
{{#if ownPost}}Edit{{/if}}
//...
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
13/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
14/24
//...
Meteor.methods({
post: function(postAttributes) {
//...},
upvote: function(postId) {
check(this.userId,String);
check(postId,String);
varaffected =Posts.update({
_id: postId,
upvoters: {$ne: this.userId}
}, {
$addToSet: {upvoters: this.userId},
$inc: {votes:1}
});
if(!affected)
throw newMeteor.Error('invalid',"You weren't able to upvote t
}
});
//...
id
id
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
15/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
16/24
home newPosts NewPostsListController
{submitted: -1} PostsListController
this.sort NewPostsListController
BestPostsListController
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
17/24
//...
PostsListController =RouteController.extend({
template:'postsList',
increment:5,postsLimit: function() {
returnparseInt(this.params.postsLimit) || this.increment;
},
findOptions: function() {
return{sort: this.sort,limit: this.postsLimit()};
},
subscriptions: function() {
this.postsSub =Meteor.subscribe('posts', this.findOptions());
},
posts: function() {
returnPosts.find({}, this.findOptions());
},
data: function() {
varhasMore = this.posts().count() === this.postsLimit();
return{
posts: this.posts(),
ready: this.postsSub.ready,
nextPath: hasMore ? this.nextPath() : null
};
}
});
NewPostsController =PostsListController.extend({
sort: {submitted: -1,_id: -1},
nextPath: function() {
returnRouter.routes.newPosts.path({postsLimit: this.postsLimit()
}});
BestPostsController =PostsListController.extend({
sort: {votes: -1,submitted: -1,_id: -1},
nextPath: function() {
returnRouter.routes.bestPosts.path({postsLimit: this.postsLimit()
}
});
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
18/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
19/24
loginButtons}}
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
20/24
'click .delete': function(e) {
e.preventDefault();
if (confirm("Delete this post?")) {
var currentPostId = this._id;
Posts.remove(currentPostId);
Router.go('home');
}
}
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
21/24
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
22/24
loginButtons}}
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
23/24
Template.header.helpers({
activeRouteClass: function(/* route names */) {
varargs =Array.prototype.slice.call(arguments,0);args.pop();
varactive =_.any(args, function(name) {
returnRouter.current() &&Router.current().route.getName() ===
});
returnactive &&'active';
}
});
nng b phiu - Discover Meteor http://vi.discovermeteor.com/chapte
24 1/10/2016
-
7/23/2019 Voting in Discover Meteor Vietnamese Version
24/24