Fix deletion of Post Model in Editor and Content screens

fixes #2999
- handle undefined argument in openModal function
- catch whether a model is deleted in Editor routes to aid transition
- move updateTags function to the PostModel
- add call to updateTags in delete-post modal
This commit is contained in:
David Arvelo 2014-06-19 14:31:56 -04:00
parent f9e363dda3
commit b891b2b778
7 changed files with 43 additions and 25 deletions

View File

@ -1,9 +1,13 @@
var DeletePostController = Ember.Controller.extend({ var DeletePostController = Ember.Controller.extend({
actions: { actions: {
confirmAccept: function () { confirmAccept: function () {
var self = this; var self = this,
model = this.get('model');
this.get('model').destroyRecord().then(function () { // definitely want to clear the data store and post of any unsaved, client-generated tags
model.updateTags();
model.destroyRecord().then(function () {
self.get('popover').closePopovers(); self.get('popover').closePopovers();
self.notifications.showSuccess('Your post has been deleted.'); self.notifications.showSuccess('Your post has been deleted.');
self.transitionToRoute('posts.index'); self.transitionToRoute('posts.index');

View File

@ -20,7 +20,7 @@ var LeaveEditorController = Ember.Controller.extend({
} }
// definitely want to clear the data store and post of any unsaved, client-generated tags // definitely want to clear the data store and post of any unsaved, client-generated tags
editorController.updateTags(); model.updateTags();
if (model.get('isNew')) { if (model.get('isNew')) {
// the user doesn't want to save the new, unsaved post, so delete it. // the user doesn't want to save the new, unsaved post, so delete it.

View File

@ -106,16 +106,6 @@ var EditorControllerMixin = Ember.Mixin.create(MarkerManager, {
'=============================='; '==============================';
}, },
// remove client-generated tags, which have `id: null`.
// Ember Data won't recognize/update them automatically
// when returned from the server with ids.
updateTags: function () {
var tags = this.get('model.tags'),
oldTags = tags.filterBy('id', null);
tags.removeObjects(oldTags);
oldTags.invoke('deleteRecord');
},
actions: { actions: {
save: function () { save: function () {
var status = this.get('willPublish') ? 'published' : 'draft', var status = this.get('willPublish') ? 'published' : 'draft',
@ -126,7 +116,7 @@ var EditorControllerMixin = Ember.Mixin.create(MarkerManager, {
this.set('status', status); this.set('status', status);
return this.get('model').save().then(function (model) { return this.get('model').save().then(function (model) {
self.updateTags(); model.updateTags();
// `updateTags` triggers `isDirty => true`. // `updateTags` triggers `isDirty => true`.
// for a saved model it would otherwise be false. // for a saved model it would otherwise be false.
self.set('isDirty', false); self.set('isDirty', false);

View File

@ -34,7 +34,18 @@ var Post = DS.Model.extend({
} }
return validationErrors; return validationErrors;
}.property('title') }.property('title'),
// remove client-generated tags, which have `id: null`.
// Ember Data won't recognize/update them automatically
// when returned from the server with ids.
updateTags: function () {
var tags = this.get('tags'),
oldTags = tags.filterBy('id', null);
tags.removeObjects(oldTags);
oldTags.invoke('deleteRecord');
}
}); });
export default Post; export default Post;

View File

@ -30,8 +30,11 @@ var ApplicationRoute = Ember.Route.extend({
// so we're skipping asserting if one exists // so we're skipping asserting if one exists
if (this.controllerFor(modalName, true)) { if (this.controllerFor(modalName, true)) {
this.controllerFor(modalName).set('model', model); this.controllerFor(modalName).set('model', model);
this.controllerFor(modalName).set('imageType', type);
this.controllerFor(modalName).set('src', model.get(type)); if (type) {
this.controllerFor(modalName).set('imageType', type);
this.controllerFor(modalName).set('src', model.get(type));
}
} }
return this.render(modalName, { return this.render(modalName, {
into: 'application', into: 'application',

View File

@ -57,12 +57,17 @@ var EditorEditRoute = AuthenticatedRoute.extend(styleBody, {
model = controller.get('model'), model = controller.get('model'),
isSaving = model.get('isSaving'), isSaving = model.get('isSaving'),
isDeleted = model.get('isDeleted'); isDeleted = model.get('isDeleted'),
modelIsDirty = model.get('isDirty');
// when `isDeleted && isSaving`, model is in-flight, being saved // when `isDeleted && isSaving`, model is in-flight, being saved
// to the server. in that case we can probably just transition // to the server. when `isDeleted && !isSaving && !modelIsDirty`,
// now and have the server return the record, thereby updating it // the record has already been deleted and the deletion persisted.
if (!(isDeleted && isSaving) && isDirty) { //
// 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(); transition.abort();
this.send('openModal', 'leave-editor', [controller, transition]); this.send('openModal', 'leave-editor', [controller, transition]);
return; return;

View File

@ -24,12 +24,17 @@ var EditorNewRoute = AuthenticatedRoute.extend(styleBody, {
model = controller.get('model'), model = controller.get('model'),
isNew = model.get('isNew'), isNew = model.get('isNew'),
isSaving = model.get('isSaving'), isSaving = model.get('isSaving'),
isDeleted = model.get('isDeleted'); isDeleted = model.get('isDeleted'),
modelIsDirty = model.get('isDirty');
// when `isDeleted && isSaving`, model is in-flight, being saved // when `isDeleted && isSaving`, model is in-flight, being saved
// to the server. in that case we can probably just transition // to the server. when `isDeleted && !isSaving && !modelIsDirty`,
// now and have the server return the record, thereby updating it // the record has already been deleted and the deletion persisted.
if (!(isDeleted && isSaving) && isDirty) { //
// 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(); transition.abort();
this.send('openModal', 'leave-editor', [controller, transition]); this.send('openModal', 'leave-editor', [controller, transition]);
return; return;