Ghost/core/client/routes/editor/edit.js
David Arvelo 85f7e1d269 Get single post from API by id and query params; Auto-sort posts list
closes #2883, closes #2951
- introduce custom findQuery in ApplicationAdapter
- posts/post route and editor/edit route now use custom findQuery to find a single post by id with query params
- create a sorting function in PostsController for out-of-order loading
- refresh updated posts in posts.index to make PostsList highlight latest draft after returning from a save in editor
2014-06-17 18:10:11 -04:00

78 lines
2.3 KiB
JavaScript

import styleBody from 'ghost/mixins/style-body';
import AuthenticatedRoute from 'ghost/routes/authenticated';
var EditorEditRoute = AuthenticatedRoute.extend(styleBody, {
classNames: ['editor'],
model: function (params) {
var self = this,
post,
postId;
postId = Number(params.post_id);
if (!Number.isInteger(postId) || !Number.isFinite(postId) || postId <= 0) {
this.transitionTo('posts.index');
}
post = this.store.getById('post', postId);
if (post) {
return post;
}
return this.store.find('post', {
id: params.post_id,
status: 'all',
staticPages: 'all'
}).then(function (records) {
var post = records.get('firstObject');
if (post) {
return post;
}
return self.transitionTo('posts.index');
});
},
serialize: function (model) {
return {post_id: model.get('id')};
},
setupController: function (controller, model) {
this._super(controller, model);
controller.set('scratch', model.get('markdown'));
model.get('tags').then(function (tags) {
// used to check if anything has changed in the editor
controller.set('previousTagNames', tags.mapBy('name'));
});
},
actions: {
willTransition: function (transition) {
var controller = this.get('controller'),
isDirty = controller.get('isDirty'),
model = controller.get('model'),
isSaving = model.get('isSaving'),
isDeleted = model.get('isDeleted');
// when `isDeleted && isSaving`, model is in-flight, being saved
// to the server. in that case we can probably just transition
// now and have the server return the record, thereby updating it
if (!(isDeleted && isSaving) && isDirty) {
transition.abort();
this.send('openModal', 'leave-editor', [controller, transition]);
return;
}
// since the transition is now certain to complete..
window.onbeforeunload = null;
}
}
});
export default EditorEditRoute;