Ghost/core/client/routes/editor/new.js
Hannah Wolfe d159a5cced Merge pull request #3002 from novaugust/shortcuts
Implement Shortcuts in Ember
2014-06-21 15:15:50 +01:00

54 lines
1.9 KiB
JavaScript

import AuthenticatedRoute from 'ghost/routes/authenticated';
import base from 'ghost/mixins/editor-route-base';
var EditorNewRoute = AuthenticatedRoute.extend(base, {
classNames: ['editor'],
model: function () {
return this.store.createRecord('post');
},
setupController: function (controller, model) {
this._super(controller, model);
controller.set('scratch', '');
// used to check if anything has changed in the editor
controller.set('previousTagNames', Ember.A());
},
actions: {
willTransition: function (transition) {
var controller = this.get('controller'),
isDirty = controller.get('isDirty'),
model = controller.get('model'),
isNew = model.get('isNew'),
isSaving = model.get('isSaving'),
isDeleted = model.get('isDeleted'),
modelIsDirty = model.get('isDirty');
// when `isDeleted && isSaving`, model is in-flight, being saved
// to the server. when `isDeleted && !isSaving && !modelIsDirty`,
// the record has already been deleted and the deletion persisted.
//
// in either case we can probably just transition now.
// in the former case the server will return the record, thereby updating it.
// @TODO: this will break if the model fails server-side validation.
if (!(isDeleted && isSaving) && !(isDeleted && !isSaving && !modelIsDirty) && isDirty) {
transition.abort();
this.send('openModal', 'leave-editor', [controller, transition]);
return;
}
if (isNew) {
model.deleteRecord();
}
// since the transition is now certain to complete..
window.onbeforeunload = null;
}
}
});
export default EditorNewRoute;