Merge pull request #430 from jgable/fix414

Show validation error when saving post
This commit is contained in:
Hannah Wolfe 2013-08-18 15:05:32 -07:00
commit 072a3ebf61

View File

@ -87,9 +87,11 @@
}, },
toggleStatus: function () { toggleStatus: function () {
var keys = Object.keys(this.statusMap), var view = this,
keys = Object.keys(this.statusMap),
model = this.model, model = this.model,
currentIndex = keys.indexOf(model.get('status')), prevStatus = this.model.get('status'),
currentIndex = keys.indexOf(prevStatus),
newIndex; newIndex;
@ -107,18 +109,20 @@
message: 'Your post: ' + model.get('title') + ' has been ' + keys[newIndex], message: 'Your post: ' + model.get('title') + ' has been ' + keys[newIndex],
status: 'passive' status: 'passive'
}); });
}, function () { }, function (response) {
Ghost.notifications.addItem({ var status = keys[newIndex];
type: 'error', // Show a notification about the error
message: 'Your post: ' + model.get('title') + ' has not been ' + keys[newIndex], view.reportSaveError(response, model, status);
status: 'passive' // Set the button text back to previous
}); model.set({ status: prevStatus });
}); });
}, },
handleStatus: function (e) { handleStatus: function (e) {
e.preventDefault(); e.preventDefault();
var status = $(e.currentTarget).attr('data-set-status'), var view = this,
status = $(e.currentTarget).attr('data-set-status'),
prevStatus = this.model.get('status'),
model = this.model; model = this.model;
if (status === 'publish-on') { if (status === 'publish-on') {
@ -144,12 +148,11 @@
message: 'Your post: ' + model.get('title') + ' has been ' + status, message: 'Your post: ' + model.get('title') + ' has been ' + status,
status: 'passive' status: 'passive'
}); });
}, function () { }, function (response) {
Ghost.notifications.addItem({ // Show a notification about the error
type: 'error', view.reportSaveError(response, model, status);
message: 'Your post: ' + model.get('title') + ' has not been ' + status, // Set the button text back to previous
status: 'passive' model.set({ status: prevStatus });
});
}); });
}, },
@ -195,6 +198,25 @@
return $.Deferred().reject(); return $.Deferred().reject();
}, },
reportSaveError: function (response, model, status) {
var title = model.get('title') || '[Untitled]',
message = 'Your post: ' + title + ' has not been ' + status;
if (response) {
// Get message from response
message = this.getErrorMessageFromResponse(response);
} else if (model.validationError) {
// Grab a validation error
message += "; " + model.validationError;
}
Ghost.notifications.addItem({
type: 'error',
message: message,
status: 'passive'
});
},
render: function () { render: function () {
this.$('.js-post-button').text(this.statusMap[this.model.get('status')]); this.$('.js-post-button').text(this.statusMap[this.model.get('status')]);
} }