Merge branch 'master' of github.com:SachaG/Telescope

This commit is contained in:
Sacha Greif 2012-09-18 09:06:03 +09:00
commit 5ede156deb
4 changed files with 111 additions and 41 deletions

View file

@ -125,6 +125,10 @@ Template.comment_item.rendered=function(){
}
);
// $(event.target).closest(".comment").addClass("queued");
},
'click .upvote': function(e) {
e.preventDefault();
Meteor.call('up voteComment', this._id);
}
};

View file

@ -8,7 +8,7 @@ Template.post_item.events = {
, 'click .upvote-link': function(){
console.log('upvote', this);
Meteor.call('voteForPost', this);
Meteor.call('upvotePost', this._id);
}
, 'click .share-link': function(e){
@ -82,7 +82,7 @@ Template.post_item.voted = function(){
var user = Meteor.user();
if(!user) return false;
return _.include(this.voters, user._id);
return _.include(this.upvoters, user._id);
};
var getRank = function(post){

View file

@ -1,21 +1,87 @@
Meteor.methods({
voteForPost: function(post){
console.log('voting for ' + post._id);
(function() {
var prepareVote = function(collection, id, fn) {
// ensure we are logged in
console.log('voting for ' + id);
var userId = this.userId();
if(!userId) return false;
// atomically update the post's votes
var query = {_id: post._id, voters: {$ne: userId}};
var update = {$push: {voters: userId}, $inc: {votes: 1}};
Posts.update(query, update);
// now do the real work
fn(userId);
// now update scores
if (!this.is_simulation) {
// now update the post's score
post = Posts.findOne(post._id);
Scoring.updateObject(post);
Posts.update(post._id, {$set: {score: post.score}});
var object = collection.findOne(id);
Scoring.updateObject(object);
collection.update(id, {$set: {score: object.score}});
}
return true;
}
});
};
var upvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var query = {_id: id, upvoters: {$ne: userId}};
var update = {$push: {upvoters: userId}, $inc: {votes: 1}};
collection.update(query, update);
});
};
var downvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var query = {_id: id, downvoters: {$ne: userId}};
var update = {$push: {downvoters: userId}, $inc: {votes: -1}};
collection.update(query, update);
});
};
var cancelUpvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var query = {_id: id, upvoters: userId};
var update = {$pull: {upvoters: userId}, $inc: {votes: -1}};
collection.update(query, update);
});
};
var cancelDownvote = function(collection, id) {
return prepareVote.call(this, collection, id, function(userId) {
var query = {_id: id, downvoters: userId};
var update = {$pull: {downvoters: userId}, $inc: {votes: 1}};
collection.update(query, update);
});
};
Meteor.methods({
upvotePost: function(postId){
return upvote.call(this, Posts, postId);
},
downvotePost: function(postId){
return downvote.call(this, Posts, postId);
},
cancelUpvotePost: function(postId){
return cancelUpvote.call(this, Posts, postId);
},
cancelDownvotePost: function(postId){
return cancelDownvote.call(this, Posts, postId);
},
upvoteComment: function(commentId){
return upvote.call(this, Comments, commentId);
},
downvoteComment: function(commentId){
return downvote.call(this, Comments, commentId);
},
cancelUpvoteComment: function(commentId){
return cancelUpvote.call(this, Comments, commentId);
},
cancelDownvoteComment: function(commentId){
return cancelDownvote.call(this, Comments, commentId);
}
});
})();

54
server/bootstrap.js vendored
View file

@ -1,28 +1,28 @@
// function prepopulateDatabase(){
// [
// {
// headline: 'The first post ever, a link to Hacker News'
// , url: 'http://news.ycombinator.com/'
// , submitter: 'Sacha'
// , submitted: new Date(2012, 7, 22).getTime()
// , votes: 0
// , comments: 0
// }
// , {
// headline: 'Another post to fill the page up a little'
// , url: 'http://sachagreif.com/'
// , submitter: 'Sacha'
// , submitted: new Date(2012, 7, 22).getTime()
// , votes: 0
// , comments: 0
// }
// ].forEach(function(post){
// Posts.insert(post);
// });
// }
function prepopulateDatabase(){
[
{
headline: 'The first post ever, a link to Hacker News'
, url: 'http://news.ycombinator.com/'
, submitter: 'Sacha'
, submitted: new Date(2012, 7, 22).getTime()
, votes: 0
, comments: 0
}
, {
headline: 'Another post to fill the page up a little'
, url: 'http://sachagreif.com/'
, submitter: 'Sacha'
, submitted: new Date(2012, 7, 22).getTime()
, votes: 0
, comments: 0
}
].forEach(function(post){
Posts.insert(post);
});
}
// Meteor.startup(function () {
// if(Posts.find().count() === 0){
// prepopulateDatabase();
// }
// });
Meteor.startup(function () {
if(Posts.find().count() === 0){
prepopulateDatabase();
}
});