diff --git a/core/client/controllers/post-settings-menu.js b/core/client/controllers/post-settings-menu.js index 2b9b1d4a31..fa1f784e65 100644 --- a/core/client/controllers/post-settings-menu.js +++ b/core/client/controllers/post-settings-menu.js @@ -7,6 +7,8 @@ var PostSettingsMenuController = Ember.ObjectController.extend({ //State for if the user is viewing a tab's pane. needs: 'application', + lastPromise: null, + isViewingSubview: Ember.computed('controllers.application.showRightOutlet', function (key, value) { // Not viewing a subview if we can't even see the PSM if (!this.get('controllers.application.showRightOutlet')) { @@ -89,11 +91,22 @@ var PostSettingsMenuController = Ember.ObjectController.extend({ //Requests slug from title generateAndSetSlug: function (destination) { var self = this, - title = this.get('titleScratch'); + title = this.get('titleScratch'), + afterSave = this.get('lastPromise'), + promise; - this.get('slugGenerator').generateSlug(title).then(function (slug) { - self.set(destination, slug); + // Only set an "untitled" slug once per post + if (title === '(Untitled)' && this.get('slug')) { + return; + } + + promise = Ember.RSVP.resolve(afterSave).then(function () { + return self.get('slugGenerator').generateSlug(title).then(function (slug) { + self.set(destination, slug); + }); }); + + this.set('lastPromise', promise); }, metaTitleScratch: boundOneWay('meta_title'), @@ -168,11 +181,15 @@ var PostSettingsMenuController = Ember.ObjectController.extend({ } }.observes('model'), titleObserver: function () { + var debounceId; + if (this.get('isNew') && !this.get('title')) { - Ember.run.debounce(this, 'generateAndSetSlug', ['slugPlaceholder'], 700); + debounceId = Ember.run.debounce(this, 'generateAndSetSlug', ['slugPlaceholder'], 700); } else if (this.get('title') === '(Untitled)') { - Ember.run.debounce(this, 'generateAndSetSlug', ['slug'], 700); + debounceId = Ember.run.debounce(this, 'generateAndSetSlug', ['slug'], 700); } + + this.set('debounceId', debounceId); }, slugPlaceholder: Ember.computed(function (key, value) { var slug = this.get('slug'); diff --git a/core/client/mixins/editor-base-controller.js b/core/client/mixins/editor-base-controller.js index f557bd059e..f0f40b6f68 100644 --- a/core/client/mixins/editor-base-controller.js +++ b/core/client/mixins/editor-base-controller.js @@ -12,7 +12,7 @@ PostModel.eachAttribute(function (name) { }); var EditorControllerMixin = Ember.Mixin.create(MarkerManager, { - needs: ['post-tags-input'], + needs: ['post-tags-input', 'post-settings-menu'], init: function () { var self = this; @@ -195,7 +195,9 @@ var EditorControllerMixin = Ember.Mixin.create(MarkerManager, { prevStatus = this.get('status'), isNew = this.get('isNew'), autoSaveId = this.get('autoSaveId'), - self = this; + self = this, + psmController = this.get('controllers.post-settings-menu'), + promise; options = options || {}; @@ -221,11 +223,21 @@ var EditorControllerMixin = Ember.Mixin.create(MarkerManager, { this.set('title', this.get('titleScratch')); - return this.get('model').save(options).then(function (model) { - if (!options.silent) { - self.showSaveNotification(prevStatus, model.get('status'), isNew ? true : false); - } - return model; + if (!this.get('slug')) { + // Cancel any pending slug generation that may still be queued in the + // run loop because we need to run it before the post is saved. + Ember.run.cancel(psmController.get('debounceId')); + + psmController.generateAndSetSlug('slug'); + } + + promise = Ember.RSVP.resolve(psmController.get('lastPromise')).then(function () { + return self.get('model').save(options).then(function (model) { + if (!options.silent) { + self.showSaveNotification(prevStatus, model.get('status'), isNew ? true : false); + } + return model; + }); }).catch(function (errors) { if (!options.silent) { self.showErrorNotification(prevStatus, self.get('status'), errors); @@ -234,6 +246,10 @@ var EditorControllerMixin = Ember.Mixin.create(MarkerManager, { return Ember.RSVP.reject(errors); }); + + psmController.set('lastPromise', promise); + + return promise; }, setSaveType: function (newType) {