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

85 lines
2.4 KiB
JavaScript
Raw Normal View History

Template[getTemplate('post_edit')].helpers({
postFields: function () {
var post = this.post;
var fields = [];
_.each(postSchemaObject, function (property, key) {
if ((property.autoform && !!property.autoform.omit) || key.indexOf('$') != -1) {
// if field is set to "omit" or contains a "$", stop right here
// this is not actually necessary since these fields will be omitted anyway, but this way
// the "fields" array will reflect what gets displayed
return
}
if (isAdmin(Meteor.user())) {
// if user is admin, display field
fields.push(key);
} else if (property.editableBy && _.contains(property.editableBy, "owner") && isOwner(Meteor.user(), post)) {
// else, if a field is editable by its owner display it
fields.push(key);
}
});
return fields;
}
});
2014-11-29 10:25:11 +09:00
AutoForm.hooks({
editPostForm: {
before: {
2015-03-11 08:34:44 +09:00
editPost: function(modifier) {
console.log(modifier)
console.log(template)
var post = doc;
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
// run all post edit client callbacks on modifier object successively
post = Telescope.callbacks.postEditClient.reduce(function(result, currentFunction) {
return currentFunction(result);
}, post);
return post;
}
2014-11-29 10:25:11 +09:00
},
2015-03-11 08:34:44 +09:00
onSuccess: function(operation, post) {
Events.track("edit post", {'postId': post._id});
Router.go('post_page', {_id: post._id});
},
2014-11-29 10:25:11 +09:00
2015-03-11 08:34:44 +09:00
onError: function(operation, error) {
2014-11-29 10:25:11 +09:00
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
}
2014-12-08 16:10:32 +09:00
});
// delete link
Template.post_edit.events({
2014-12-08 16:10:32 +09:00
'click .delete-link': function(e){
var post = this.post;
e.preventDefault();
2015-03-27 16:24:21 +08:00
2014-12-08 16:10:32 +09:00
if(confirm("Are you sure?")){
Router.go("/");
Meteor.call("deletePostById", post._id, function(error) {
if (error) {
console.log(error);
2015-03-27 16:24:21 +08:00
Messages.flash(error.reason, 'error');
2014-12-08 16:10:32 +09:00
} else {
2015-03-27 16:24:21 +08:00
Messages.flash(i18n.t('your_post_has_been_deleted'), 'success');
2014-12-08 16:10:32 +09:00
}
});
}
}
2015-03-27 16:24:21 +08:00
});