The last of the callback style AJAX have been converted to promises.

This commit is contained in:
Robin Ward 2013-05-07 10:58:41 -04:00
parent dfd423a188
commit 6cbcd6e4a6
7 changed files with 31 additions and 45 deletions

View File

@ -73,7 +73,7 @@ Discourse.MessageBus = (function() {
data[c.channel] = c.last_id === void 0 ? -1 : c.last_id; data[c.channel] = c.last_id === void 0 ? -1 : c.last_id;
}); });
gotData = false; gotData = false;
_this.longPoll = Discourse.ajax(Discourse.getURL("/message-bus/") + clientId + "/poll?" + (!shouldLongPoll() || !_this.enableLongPolling ? "dlp=t" : ""), { _this.longPoll = $.ajax(Discourse.getURL("/message-bus/") + clientId + "/poll?" + (!shouldLongPoll() || !_this.enableLongPolling ? "dlp=t" : ""), {
data: data, data: data,
cache: false, cache: false,
dataType: 'json', dataType: 'json',

View File

@ -35,7 +35,7 @@ Discourse.Category = Discourse.Model.extend({
url = Discourse.getURL("/categories/") + (this.get('id')); url = Discourse.getURL("/categories/") + (this.get('id'));
} }
return this.ajax(url, { return Discourse.ajax(url, {
data: { data: {
name: this.get('name'), name: this.get('name'),
color: this.get('color'), color: this.get('color'),

View File

@ -9,21 +9,6 @@
**/ **/
Discourse.Model = Ember.Object.extend(Discourse.Presence, { Discourse.Model = Ember.Object.extend(Discourse.Presence, {
/**
Our own AJAX handler that handles erronous responses
@method ajax
@param {String} url The url to contact
@param {Object} args The arguments to pass to $.ajax
**/
ajax: function(url, args) {
var oldError = args.error;
args.error = function(xhr) {
return oldError($.parseJSON(xhr.responseText).errors);
};
return Discourse.ajax(url, args);
},
/** /**
Update our object from another object Update our object from another object

View File

@ -83,22 +83,23 @@ Discourse.Post = Discourse.Model.extend({
return Em.String.i18n('bookmarks.not_bookmarked'); return Em.String.i18n('bookmarks.not_bookmarked');
}).property('read', 'topic.last_read_post_number', 'bookmarked'), }).property('read', 'topic.last_read_post_number', 'bookmarked'),
bookmarkedChanged: (function() { bookmarkedChanged: function() {
var _this = this; var post = this;
return Discourse.ajax({ Discourse.ajax({
url: Discourse.getURL("/posts/") + (this.get('id')) + "/bookmark", url: Discourse.getURL("/posts/") + (this.get('id')) + "/bookmark",
type: 'PUT', type: 'PUT',
data: { data: {
bookmarked: this.get('bookmarked') ? true : false bookmarked: this.get('bookmarked') ? true : false
}, }
error: function(error) { }).fail(function (error) {
var errors; if (error && error.responseText) {
errors = $.parseJSON(error.responseText).errors; bootbox.alert($.parseJSON(error.responseText).errors[0]);
bootbox.alert(errors[0]); } else {
return _this.toggleProperty('bookmarked'); bootbox.alert(Em.String.i18n('generic_error'));
} }
}); });
}).observes('bookmarked'),
}.observes('bookmarked'),
internalLinks: (function() { internalLinks: (function() {
if (this.blank('link_counts')) return null; if (this.blank('link_counts')) return null;

View File

@ -160,11 +160,14 @@ Discourse.Topic = Discourse.Model.extend({
return Discourse.ajax({ return Discourse.ajax({
url: "" + (this.get('url')) + "/star", url: "" + (this.get('url')) + "/star",
type: 'PUT', type: 'PUT',
data: { starred: topic.get('starred') ? true : false }, data: { starred: topic.get('starred') ? true : false }
error: function(error) { }).fail(function (error) {
topic.toggleProperty('starred'); topic.toggleProperty('starred');
var errors = $.parseJSON(error.responseText).errors;
return bootbox.alert(errors[0]); if (error && error.responseText) {
bootbox.alert($.parseJSON(error.responseText).errors);
} else {
bootbox.alert(Em.String.i18n('generic_error'));
} }
}); });
}, },
@ -349,11 +352,10 @@ Discourse.Topic = Discourse.Model.extend({
topic.set('pinned', false); topic.set('pinned', false);
Discourse.ajax(Discourse.getURL("/t/") + this.get('id') + "/clear-pin", { Discourse.ajax(Discourse.getURL("/t/") + this.get('id') + "/clear-pin", {
type: 'PUT', type: 'PUT'
error: function() { }).fail(function() {
// On error, put the pin back // On error, put the pin back
topic.set('pinned', true); topic.set('pinned', true);
}
}); });
}, },

View File

@ -159,11 +159,9 @@ Discourse.User = Discourse.Model.extend({
'new_topic_duration_minutes', 'new_topic_duration_minutes',
'external_links_in_new_tab', 'external_links_in_new_tab',
'enable_quoting'), 'enable_quoting'),
type: 'PUT', type: 'PUT'
success: function(data) { }).then(function(data) {
user.set('bio_excerpt',data.user.bio_excerpt); user.set('bio_excerpt',data.user.bio_excerpt);
}
}).then(function() {
Discourse.set('currentUser.enable_quoting', user.get('enable_quoting')); Discourse.set('currentUser.enable_quoting', user.get('enable_quoting'));
Discourse.set('currentUser.external_links_in_new_tab', user.get('external_links_in_new_tab')); Discourse.set('currentUser.external_links_in_new_tab', user.get('external_links_in_new_tab'));
}); });

View File

@ -3,7 +3,7 @@
[ [
{ {
"path": "app", "path": "app",
"folder_exclude_patterns": ["external", "external_production", "images", "imported", "fonts", "defer"], "folder_exclude_patterns": ["external", "external_development", "external_production", "images", "imported", "fonts", "defer"],
"file_exclude_patterns": ["i18n.js"] "file_exclude_patterns": ["i18n.js"]
}, },
{ "path": "config" }, { "path": "config" },