Vulcan/client/views/comments/comment_form.js

88 lines
1.8 KiB
JavaScript
Raw Normal View History

2014-07-05 11:24:28 +09:00
Template[getTemplate('comment_form')].helpers({
2014-06-27 19:42:33 +09:00
canComment: function(){
return canComment(Meteor.user());
}
});
2014-06-27 19:42:33 +09:00
2014-07-05 11:24:28 +09:00
Template[getTemplate('comment_form')].events({
2013-04-26 17:28:09 +09:00
'submit form': function(e, instance){
2014-12-24 10:13:48 +09:00
e.preventDefault();
2012-10-01 18:48:46 +09:00
$(e.target).addClass('disabled');
clearSeenMessages();
2014-12-24 10:13:48 +09:00
2014-12-27 18:34:01 +09:00
2014-12-24 10:13:48 +09:00
var comment = {};
var $commentForm = instance.$('#comment');
2014-12-27 18:34:01 +09:00
var $submitButton = instance.$('.btn-submit');
2014-12-24 10:13:48 +09:00
var body = $commentForm.val();
2014-12-27 18:34:01 +09:00
$submitButton.addClass('loading');
if(getCurrentTemplate() == 'comment_reply'){
2014-12-24 10:13:48 +09:00
2014-12-27 22:12:01 +09:00
var parentComment = this.comment;
2013-04-26 17:28:09 +09:00
// child comment
2014-12-24 10:13:48 +09:00
comment = {
postId: parentComment.postId,
parentCommentId: parentComment._id,
body: body
};
Meteor.call('submitComment', comment, function(error, newComment){
2014-12-27 18:34:01 +09:00
$submitButton.removeClass('loading');
2014-12-24 10:13:48 +09:00
if (error) {
2013-10-09 20:11:58 +09:00
console.log(error);
flashMessage(error.reason, "error");
2014-12-24 10:13:48 +09:00
} else {
trackEvent("newComment", newComment);
2014-12-24 10:13:48 +09:00
Router.go('post_page_comment', {
_id: parentComment.postId,
commentId: newComment._id
});
2014-12-24 10:13:48 +09:00
2013-10-09 20:11:58 +09:00
}
2014-12-24 10:13:48 +09:00
2013-04-26 17:28:09 +09:00
});
2014-12-24 10:13:48 +09:00
}else{
2014-12-24 10:13:48 +09:00
2013-04-26 17:28:09 +09:00
// root comment
var post = postObject;
2012-10-04 14:54:26 +09:00
2014-12-24 10:13:48 +09:00
comment = {
postId: post._id,
body: body
}
Meteor.call('submitComment', comment, function(error, newComment){
2014-12-27 18:34:01 +09:00
$commentForm.val('');
$submitButton.removeClass('loading');
2013-10-09 20:11:58 +09:00
if(error){
2014-12-24 10:13:48 +09:00
2013-10-09 20:11:58 +09:00
console.log(error);
flashMessage(error.reason, "error");
2014-12-24 10:13:48 +09:00
2013-10-09 20:11:58 +09:00
}else{
2014-12-24 10:13:48 +09:00
trackEvent("newComment", newComment);
Session.set('scrollToCommentId', newComment._id);
2014-12-24 10:13:48 +09:00
2013-10-09 20:11:58 +09:00
}
2014-12-24 10:13:48 +09:00
2013-04-26 17:28:09 +09:00
});
}
}
});