2014-06-19 23:44:44 +04:00
|
|
|
import base from 'ghost/mixins/editor-route-base';
|
2014-03-10 07:44:08 +04:00
|
|
|
|
2014-07-25 17:38:13 +04:00
|
|
|
var EditorNewRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, base, {
|
2014-03-10 07:44:08 +04:00
|
|
|
classNames: ['editor'],
|
|
|
|
|
2014-05-21 17:53:00 +04:00
|
|
|
model: function () {
|
2014-07-17 09:54:32 +04:00
|
|
|
var self = this;
|
|
|
|
return this.get('session.user').then(function (user) {
|
|
|
|
return self.store.createRecord('post', {
|
|
|
|
author: user
|
|
|
|
});
|
|
|
|
});
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setupController: function (controller, model) {
|
|
|
|
this._super(controller, model);
|
2014-10-26 02:29:58 +03:00
|
|
|
|
|
|
|
var psm = this.controllerFor('post-settings-menu');
|
|
|
|
|
|
|
|
// make sure there are no titleObserver functions hanging around
|
|
|
|
// from previous posts
|
|
|
|
psm.removeObserver('titleScratch', psm, 'titleObserver');
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
controller.set('scratch', '');
|
2014-09-24 00:04:26 +04:00
|
|
|
controller.set('titleScratch', '');
|
2014-06-06 05:18:03 +04:00
|
|
|
|
|
|
|
// used to check if anything has changed in the editor
|
|
|
|
controller.set('previousTagNames', Ember.A());
|
2014-06-28 07:01:56 +04:00
|
|
|
|
|
|
|
// attach model-related listeners created in editor-route-base
|
|
|
|
this.attachModelHooks(controller, model);
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
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'),
|
2014-06-19 22:31:56 +04:00
|
|
|
isDeleted = model.get('isDeleted'),
|
|
|
|
modelIsDirty = model.get('isDirty');
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2014-10-13 19:23:06 +04:00
|
|
|
this.send('closeSettingsMenu');
|
2014-09-15 04:40:24 +04:00
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// when `isDeleted && isSaving`, model is in-flight, being saved
|
2014-06-19 22:31:56 +04:00
|
|
|
// 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) {
|
2014-06-06 05:18:03 +04:00
|
|
|
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;
|
2014-06-28 07:01:56 +04:00
|
|
|
|
|
|
|
// remove model-related listeners created in editor-route-base
|
|
|
|
this.detachModelHooks(controller, model);
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
2014-03-10 07:44:08 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-06-10 08:44:29 +04:00
|
|
|
export default EditorNewRoute;
|