From 39967b02da7a7936767dadecf497817d867707e1 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Tue, 3 Jun 2014 21:10:54 +0000 Subject: [PATCH] Enable post deletion from Ember admin Closes #2849 -wire up delete post action in ember admin -refactor ember modal dialog -override RESTAdapter.deleteRecord to workaround Ember expecting an empty response body on DELETEs --- core/client/adapters/application.js | 14 +++++ core/client/components/gh-modal-dialog.js | 8 +-- core/client/controllers/modals/delete-all.js | 51 +++++-------------- core/client/controllers/modals/delete-post.js | 46 +++++++---------- core/client/controllers/modals/upload.js | 11 ++-- core/client/templates/editor-save-button.hbs | 2 +- core/client/templates/post-settings-menu.hbs | 2 +- core/server/api/index.js | 1 + 8 files changed, 58 insertions(+), 77 deletions(-) diff --git a/core/client/adapters/application.js b/core/client/adapters/application.js index ac2435c5fd..3e0fbaa3ef 100644 --- a/core/client/adapters/application.js +++ b/core/client/adapters/application.js @@ -18,5 +18,19 @@ export default DS.RESTAdapter.extend({ } return url; + }, + + // Override deleteRecord to disregard the response body on 2xx responses. + // This is currently needed because the API is returning status 200 along + // with the JSON object for the deleted entity and Ember expects an empty + // response body for successful DELETEs. + // Non-2xx (failure) responses will still work correctly as Ember will turn + // them into rejected promises. + deleteRecord: function () { + var response = this._super.apply(this, arguments); + + return response.then(function () { + return null; + }); } }); \ No newline at end of file diff --git a/core/client/components/gh-modal-dialog.js b/core/client/components/gh-modal-dialog.js index 1fca4218f5..b9f37b3f38 100644 --- a/core/client/components/gh-modal-dialog.js +++ b/core/client/components/gh-modal-dialog.js @@ -18,15 +18,15 @@ var ModalDialog = Ember.Component.extend({ return this._super(); }, + confirmaccept: 'confirmAccept', + confirmreject: 'confirmReject', + actions: { closeModal: function () { this.sendAction(); }, confirm: function (type) { - var func = this.get('confirm.' + type + '.func'); - if (typeof func === 'function') { - func(); - } + this.sendAction('confirm' + type); this.sendAction(); } }, diff --git a/core/client/controllers/modals/delete-all.js b/core/client/controllers/modals/delete-all.js index 8719a3c30d..b63571f54a 100644 --- a/core/client/controllers/modals/delete-all.js +++ b/core/client/controllers/modals/delete-all.js @@ -1,51 +1,24 @@ /*global alert */ var DeleteAllController = Ember.Controller.extend({ + actions: { + confirmAccept: function () { + alert('Deleting everything!'); + + this.notifications.showSuccess('Everything has been deleted.'); + }, + + confirmReject: function () { + return true; + } + }, + confirm: { accept: { - func: function () { - // @TODO make the below real :) - alert('Deleting everything!'); - // $.ajax({ - // url: Ghost.paths.apiRoot + '/db/', - // type: 'DELETE', - // headers: { - // 'X-CSRF-Token': $("meta[name='csrf-param']").attr('content') - // }, - // success: function onSuccess(response) { - // if (!response) { - // throw new Error('No response received from server.'); - // } - // if (!response.message) { - // throw new Error(response.detail || 'Unknown error'); - // } - - // Ghost.notifications.addItem({ - // type: 'success', - // message: response.message, - // status: 'passive' - // }); - - // }, - // error: function onError(response) { - // var responseText = JSON.parse(response.responseText), - // message = responseText && responseText.error ? responseText.error : 'unknown'; - // Ghost.notifications.addItem({ - // type: 'error', - // message: ['A problem was encountered while deleting content from your blog. Error: ', message].join(''), - // status: 'passive' - // }); - - // } - // }); - }, text: 'Delete', buttonClass: 'button-delete' }, reject: { - func: function () { - return true; - }, text: 'Cancel', buttonClass: 'button' } diff --git a/core/client/controllers/modals/delete-post.js b/core/client/controllers/modals/delete-post.js index 69af6e512d..5f59265db4 100644 --- a/core/client/controllers/modals/delete-post.js +++ b/core/client/controllers/modals/delete-post.js @@ -1,38 +1,28 @@ -/*global alert */ - var DeletePostController = Ember.Controller.extend({ + needs: 'posts/post', + actions: { + confirmAccept: function () { + var self = this; + + this.get('model').destroyRecord().then(function () { + self.notifications.showSuccess('Your post has been deleted.'); + self.transitionToRoute('posts.index'); + }, function () { + self.notifications.showError('Your post could not be deleted. Please try again.'); + }); + + }, + + confirmReject: function () { + return false; + } + }, confirm: { accept: { - func: function () { - // @TODO: make this real - alert('Deleting post'); - // self.model.destroy({ - // wait: true - // }).then(function () { - // // Redirect to content screen if deleting post from editor. - // if (window.location.pathname.indexOf('editor') > -1) { - // window.location = Ghost.paths.subdir + '/ghost/content/'; - // } - // Ghost.notifications.addItem({ - // type: 'success', - // message: 'Your post has been deleted.', - // status: 'passive' - // }); - // }, function () { - // Ghost.notifications.addItem({ - // type: 'error', - // message: 'Your post could not be deleted. Please try again.', - // status: 'passive' - // }); - // }); - }, text: 'Delete', buttonClass: 'button-delete' }, reject: { - func: function () { - return true; - }, text: 'Cancel', buttonClass: 'button' } diff --git a/core/client/controllers/modals/upload.js b/core/client/controllers/modals/upload.js index 6058de6e86..c224440eea 100644 --- a/core/client/controllers/modals/upload.js +++ b/core/client/controllers/modals/upload.js @@ -1,14 +1,17 @@ var UploadController = Ember.Controller.extend({ + actions: { + confirmReject: function () { + return true; + } + }, + confirm: { reject: { - func: function () { // The function called on rejection - return true; - }, buttonClass: true, text: 'Cancel' // The reject button text } } }); -export default UploadController; \ No newline at end of file +export default UploadController; diff --git a/core/client/templates/editor-save-button.hbs b/core/client/templates/editor-save-button.hbs index efec266f82..c9f45161a3 100644 --- a/core/client/templates/editor-save-button.hbs +++ b/core/client/templates/editor-save-button.hbs @@ -4,7 +4,7 @@ {{#gh-popover-button popoverName="post-save-menu" classNameBindings="open:active :options :up" title="Post Settings"}} {{/gh-popover-button}} -{{#gh-popover name="post-save-menu" closeOnClick="true" tagName="ul" classNames="editor-options overlay" publishTextBinding=view.publish-text draftTextBinding=view.draft-text}} +{{#gh-popover name="post-save-menu" closeOnClick="true" tagName="ul" classNames="editor-options overlay" publishTextBinding="view.publish-text" draftTextBinding="view.draft-text"}}
  • {{view.publishText}}
  • diff --git a/core/client/templates/post-settings-menu.hbs b/core/client/templates/post-settings-menu.hbs index a99740c473..ea5bebe95e 100644 --- a/core/client/templates/post-settings-menu.hbs +++ b/core/client/templates/post-settings-menu.hbs @@ -29,4 +29,4 @@ -Delete This Post +Delete This Post diff --git a/core/server/api/index.js b/core/server/api/index.js index e16736631e..3e088e0d85 100644 --- a/core/server/api/index.js +++ b/core/server/api/index.js @@ -223,6 +223,7 @@ http = function (apiMethod) { 'Content-Disposition': contentDispositionHeader() }); } + // #### Success // Send a properly formatting HTTP response containing the data with correct headers res.json(result || {});