From 75aeb9550f6c0c7443ae6c74d00028ab4e0504eb Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 8 Apr 2013 15:04:40 -0400 Subject: [PATCH] Convert a bunch of callbacks to promises --- .../controllers/preferences_controller.js | 45 ++++--- .../preferences_email_controller.js | 11 +- .../controllers/static_controller.js | 17 +-- .../discourse/models/action_summary.js | 12 +- .../javascripts/discourse/models/topic.js | 7 +- .../javascripts/discourse/models/user.js | 111 ++++++------------ .../templates/user/preferences.js.handlebars | 2 +- .../views/modal/create_account_view.js | 11 +- .../discourse/views/search/search_view.js | 47 +++----- .../javascripts/discourse/views/topic_view.js | 2 +- .../discourse/views/user/user_stream_view.js | 36 +++--- config/locales/client.en.yml | 7 +- 12 files changed, 134 insertions(+), 174 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/preferences_controller.js b/app/assets/javascripts/discourse/controllers/preferences_controller.js index c9923ea7778..ea83dbcfcf1 100644 --- a/app/assets/javascripts/discourse/controllers/preferences_controller.js +++ b/app/assets/javascripts/discourse/controllers/preferences_controller.js @@ -53,23 +53,26 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({ }).property(), save: function() { - var _this = this, model = this.get('content'); + var preferencesController = this; this.set('saving', true); this.set('saved', false); // Cook the bio for preview - return model.save(function(result) { - _this.set('saving', false); - if (result) { - if (Discourse.currentUser.id === model.get('id')) { - Discourse.currentUser.set('name', model.get('name')); - } - - _this.set('content.bio_cooked', Discourse.Markdown.cook(_this.get('content.bio_raw'))); - return _this.set('saved', true); - } else { - return alert('failed'); + var model = this.get('content'); + return model.save().then(function() { + // success + preferencesController.set('saving', false); + if (Discourse.currentUser.id === model.get('id')) { + Discourse.currentUser.set('name', model.get('name')); } + + preferencesController.set('content.bio_cooked', + Discourse.Markdown.cook(preferencesController.get('content.bio_raw'))); + preferencesController.set('saved', true); + }, function() { + // failed to update + preferencesController.set('saving', false); + alert(Em.String.i18n('generic_error')); }); }, @@ -79,12 +82,20 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({ }).property('saving'), changePassword: function() { - var _this = this; + var preferencesController = this; if (!this.get('passwordProgress')) { - this.set('passwordProgress', '(generating email)'); - return this.get('content').changePassword(function(message) { - _this.set('changePasswordProgress', false); - return _this.set('passwordProgress', "(" + message + ")"); + this.set('passwordProgress', Em.String.i18n("user.change_password.in_progress")); + return this.get('content').changePassword().then(function() { + // success + preferencesController.setProperties({ + changePasswordProgress: false, + passwordProgress: Em.String.i18n("user.change_password.success") + }); + }, function() { + preferencesController.setProperties({ + changePasswordProgress: false, + passwordProgress: Em.String.i18n("user.change_password.error") + }); }); } } diff --git a/app/assets/javascripts/discourse/controllers/preferences_email_controller.js b/app/assets/javascripts/discourse/controllers/preferences_email_controller.js index f25b39ed568..72a8a4de646 100644 --- a/app/assets/javascripts/discourse/controllers/preferences_email_controller.js +++ b/app/assets/javascripts/discourse/controllers/preferences_email_controller.js @@ -34,15 +34,14 @@ Discourse.PreferencesEmailController = Discourse.ObjectController.extend({ }).property('saving'), changeEmail: function() { - var _this = this; + var preferencesEmailController = this; this.set('saving', true); return this.get('content').changeEmail(this.get('newEmail')).then(function() { - return _this.set('success', true); + preferencesEmailController.set('success', true); }, function() { - /* Error - */ - _this.set('error', true); - return _this.set('saving', false); + // Error + preferencesEmailController.set('error', true); + preferencesEmailController.set('saving', false); }); } diff --git a/app/assets/javascripts/discourse/controllers/static_controller.js b/app/assets/javascripts/discourse/controllers/static_controller.js index 69f9ef2fb8f..3eb985d7271 100644 --- a/app/assets/javascripts/discourse/controllers/static_controller.js +++ b/app/assets/javascripts/discourse/controllers/static_controller.js @@ -7,26 +7,21 @@ @module Discourse **/ Discourse.StaticController = Discourse.Controller.extend({ - content: null, loadPath: function(path) { - var $preloaded, text, - _this = this; + var staticController = this; this.set('content', null); // Load from