Vulcan/packages/telescope-posts/lib/client/templates/post_edit.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-09-17 14:51:14 +09:00
Template.post_edit.onCreated(function () {
var template = this;
// initialize the reactive variables
template.ready = new ReactiveVar(false);
var postSubscription = Telescope.subsManager.subscribe('singlePost', FlowRouter.getParam("_id"));
// Autorun 3: when subscription is ready, update the data helper's terms
template.autorun(function () {
var subscriptionsReady = postSubscription.ready(); // ⚡ reactive ⚡
// if subscriptions are ready, set terms to subscriptionsTerms
if (subscriptionsReady) {
template.ready.set(true);
}
});
});
Template.post_edit.helpers({
2015-09-17 14:51:14 +09:00
ready: function () {
return Template.instance().ready.get();
},
post: function () {
return Posts.findOne(FlowRouter.getParam("_id"));
},
postFields: function () {
return Posts.simpleSchema().getEditableFields(Meteor.user());
}
});
2014-11-29 10:25:11 +09:00
AutoForm.hooks({
editPostForm: {
before: {
"method-update": function() {
var post = this.currentDoc;
var modifier = this.updateDoc;
2013-10-09 12:39:05 +09:00
// ------------------------------ Checks ------------------------------ //
2014-07-03 13:15:23 +09:00
if (!Meteor.user()) {
2015-03-27 16:24:21 +08:00
Messages.flash(i18n.t('you_must_be_logged_in'), "");
return false;
}
// ------------------------------ Callbacks ------------------------------ //
2014-07-03 13:15:23 +09:00
modifier = Telescope.callbacks.run("postEditClient", modifier, post);
return modifier;
}
2014-11-29 10:25:11 +09:00
},
onSuccess: function(formType, post) {
Events.track("edit post", {'postId': post._id});
2015-09-18 16:27:59 +09:00
FlowRouter.go('postPage', post);
},
2014-11-29 10:25:11 +09:00
onError: function(formType, error) {
console.log(error);
2015-03-27 16:24:21 +08:00
Messages.flash(error.reason.split('|')[0], "error"); // workaround because error.details returns undefined
Messages.clearSeen();
2013-01-13 09:13:25 +09:00
}
2014-11-29 10:25:11 +09:00
}
});