Vulcan/client/views/comments/comment_form.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

var submitComment = function(instance) {
var data = instance.data;
2014-06-27 19:42:33 +09:00
instance.$('form').addClass('disabled');
Messages.clearSeen();
2014-12-24 10:13:48 +09:00
var comment = {};
var $commentForm = instance.$('#comment');
var $submitButton = instance.$('.btn-submit');
var body = $commentForm.val();
2014-12-24 10:13:48 +09:00
// now that the form is latency compensated, we don't actually need to show this
// $commentForm.prop('disabled', true);
// $submitButton.addClass('loading');
2014-12-24 10:13:48 +09:00
// context can be either post, or comment property
var postId = !!data._id ? data._id: data.comment.postId;
var post = Posts.findOne(postId);
comment = {
postId: post._id,
body: body
};
2014-12-27 18:34:01 +09:00
// child comment
if (getCurrentTemplate() === 'comment_reply') {
comment.parentCommentId = data.comment._id;
}
Meteor.call('submitComment', comment, function(error, newComment){
// $commentForm.prop('disabled', false);
// $submitButton.removeClass('loading');
if(error){
console.log(error);
Messages.flash(error.reason, "error");
}else{
trackEvent("newComment", newComment);
$commentForm.val('');
}
});
};
Template[getTemplate('comment_form')].onRendered(function() {
var self = this;
2015-04-12 21:19:30 +09:00
this.$("#comment").keydown(function (e) {
if(((e.metaKey || e.ctrlKey) && e.keyCode == 13) || (e.ctrlKey && e.keyCode == 13)){
submitComment(self);
}
});
});
Template[getTemplate('comment_form')].helpers({
reason: function () {
return !!Meteor.user() ? i18n.t('sorry_you_do_not_have_the_rights_to_comments'): i18n.t('please_log_in_to_comment');
}
});
Template[getTemplate('comment_form')].events({
'submit form': function(e, instance){
e.preventDefault();
submitComment(instance);
}
});