More promises instead of callbacks.

This commit is contained in:
Robin Ward 2013-05-06 14:44:19 -04:00
parent a71a15913c
commit 397553e29c
3 changed files with 37 additions and 42 deletions

View File

@ -45,7 +45,8 @@ Discourse.UserSearch = {
cache = {}; cache = {};
} }
cacheTopicId = topicId; cacheTopicId = topicId;
var success = function(r) {
var organizeResults = function(r) {
var result = []; var result = [];
r.users.each(function(u) { r.users.each(function(u) {
if (exclude.indexOf(u.username) === -1) { if (exclude.indexOf(u.username) === -1) {
@ -58,9 +59,9 @@ Discourse.UserSearch = {
}; };
if (cache[term]) { if (cache[term]) {
success(cache[term]); organizeResults(cache[term]);
} else { } else {
debouncedSearch(term, topicId).then(success); debouncedSearch(term, topicId).then(organizeResults);
} }
return promise; return promise;
} }

View File

@ -182,11 +182,9 @@ Discourse.ComposerController = Discourse.Controller.extend({
@param {String} [opts.quote] If we're opening a reply from a quote, the quote we're making @param {String} [opts.quote] If we're opening a reply from a quote, the quote we're making
**/ **/
open: function(opts) { open: function(opts) {
var composer, promise, view,
_this = this;
if (!opts) opts = {}; if (!opts) opts = {};
var promise = opts.promise || Ember.Deferred.create();
opts.promise = promise = opts.promise || Ember.Deferred.create(); opts.promise = promise;
this.set('typedReply', false); this.set('typedReply', false);
this.set('similarTopics', null); this.set('similarTopics', null);
this.set('similarClosed', false); this.set('similarClosed', false);
@ -197,7 +195,8 @@ Discourse.ComposerController = Discourse.Controller.extend({
} }
// ensure we have a view now, without it transitions are going to be messed // ensure we have a view now, without it transitions are going to be messed
view = this.get('view'); var view = this.get('view');
var composerController = this;
if (!view) { if (!view) {
view = Discourse.ComposerView.create({ controller: this }); view = Discourse.ComposerView.create({ controller: this });
view.appendTo($('#main')); view.appendTo($('#main'));
@ -205,14 +204,14 @@ Discourse.ComposerController = Discourse.Controller.extend({
// the next runloop is too soon, need to get the control rendered and then // the next runloop is too soon, need to get the control rendered and then
// we need to change stuff, otherwise css animations don't kick in // we need to change stuff, otherwise css animations don't kick in
Em.run.next(function() { Em.run.next(function() {
return Em.run.next(function() { Em.run.next(function() {
return _this.open(opts); composerController.open(opts);
}); });
}); });
return promise; return promise;
} }
composer = this.get('content'); var composer = this.get('content');
if (composer && opts.draftKey !== composer.draftKey && composer.composeState === Discourse.Composer.DRAFT) { if (composer && opts.draftKey !== composer.draftKey && composer.composeState === Discourse.Composer.DRAFT) {
this.close(); this.close();
composer = null; composer = null;
@ -226,11 +225,8 @@ Discourse.ComposerController = Discourse.Controller.extend({
} else { } else {
opts.tested = true; opts.tested = true;
if (!opts.ignoreIfChanged) { if (!opts.ignoreIfChanged) {
this.cancel((function() { this.cancel().then(function() { composerController.open(opts); },
return _this.open(opts); function() { return promise.reject(); });
}), (function() {
return promise.reject();
}));
} }
return promise; return promise;
} }
@ -241,7 +237,7 @@ Discourse.ComposerController = Discourse.Controller.extend({
Discourse.Draft.get(opts.draftKey).then(function(data) { Discourse.Draft.get(opts.draftKey).then(function(data) {
opts.draftSequence = data.draft_sequence; opts.draftSequence = data.draft_sequence;
opts.draft = data.draft; opts.draft = data.draft;
return _this.open(opts); return composerController.open(opts);
}); });
return promise; return promise;
} }
@ -279,30 +275,27 @@ Discourse.ComposerController = Discourse.Controller.extend({
} }
}, },
cancel: function(success, fail) { cancel: function() {
var _this = this; var composerController = this;
if (this.get('content.hasMetaData') || ((this.get('content.reply') || "") !== (this.get('content.originalText') || ""))) { return Ember.Deferred.promise(function (promise) {
if (composerController.get('content.hasMetaData') ||
((composerController.get('content.reply') || "") !== (composerController.get('content.originalText') || ""))) {
bootbox.confirm(Em.String.i18n("post.abandon"), Em.String.i18n("no_value"), Em.String.i18n("yes_value"), function(result) { bootbox.confirm(Em.String.i18n("post.abandon"), Em.String.i18n("no_value"), Em.String.i18n("yes_value"), function(result) {
if (result) { if (result) {
_this.destroyDraft(); composerController.destroyDraft();
_this.close(); composerController.close();
if (typeof success === "function") { promise.resolve();
return success();
}
} else { } else {
if (typeof fail === "function") { promise.reject();
return fail();
}
} }
}); });
} else { } else {
// it is possible there is some sort of crazy draft with no body ... just give up on it // it is possible there is some sort of crazy draft with no body ... just give up on it
this.destroyDraft(); composerController.destroyDraft();
this.close(); composerController.close();
if (typeof success === "function") { promise.resolve();
success();
}
} }
});
}, },
openIfDraft: function() { openIfDraft: function() {

View File

@ -60,7 +60,7 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({
// Cook the bio for preview // Cook the bio for preview
var model = this.get('content'); var model = this.get('content');
return model.save().then(function() { return model.save().then(function() {
// success // model was saved
preferencesController.set('saving', false); preferencesController.set('saving', false);
if (Discourse.currentUser.id === model.get('id')) { if (Discourse.currentUser.id === model.get('id')) {
Discourse.currentUser.set('name', model.get('name')); Discourse.currentUser.set('name', model.get('name'));
@ -70,7 +70,7 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({
Discourse.Markdown.cook(preferencesController.get('content.bio_raw'))); Discourse.Markdown.cook(preferencesController.get('content.bio_raw')));
preferencesController.set('saved', true); preferencesController.set('saved', true);
}, function() { }, function() {
// failed to update // model failed to save
preferencesController.set('saving', false); preferencesController.set('saving', false);
alert(Em.String.i18n('generic_error')); alert(Em.String.i18n('generic_error'));
}); });
@ -86,12 +86,13 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({
if (!this.get('passwordProgress')) { if (!this.get('passwordProgress')) {
this.set('passwordProgress', Em.String.i18n("user.change_password.in_progress")); this.set('passwordProgress', Em.String.i18n("user.change_password.in_progress"));
return this.get('content').changePassword().then(function() { return this.get('content').changePassword().then(function() {
// success // password changed
preferencesController.setProperties({ preferencesController.setProperties({
changePasswordProgress: false, changePasswordProgress: false,
passwordProgress: Em.String.i18n("user.change_password.success") passwordProgress: Em.String.i18n("user.change_password.success")
}); });
}, function() { }, function() {
// password failed to change
preferencesController.setProperties({ preferencesController.setProperties({
changePasswordProgress: false, changePasswordProgress: false,
passwordProgress: Em.String.i18n("user.change_password.error") passwordProgress: Em.String.i18n("user.change_password.error")