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;
});
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,
cache: false,
dataType: 'json',

View File

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

View File

@ -9,21 +9,6 @@
**/
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

View File

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

View File

@ -160,11 +160,14 @@ Discourse.Topic = Discourse.Model.extend({
return Discourse.ajax({
url: "" + (this.get('url')) + "/star",
type: 'PUT',
data: { starred: topic.get('starred') ? true : false },
error: function(error) {
topic.toggleProperty('starred');
var errors = $.parseJSON(error.responseText).errors;
return bootbox.alert(errors[0]);
data: { starred: topic.get('starred') ? true : false }
}).fail(function (error) {
topic.toggleProperty('starred');
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);
Discourse.ajax(Discourse.getURL("/t/") + this.get('id') + "/clear-pin", {
type: 'PUT',
error: function() {
// On error, put the pin back
topic.set('pinned', true);
}
type: 'PUT'
}).fail(function() {
// On error, put the pin back
topic.set('pinned', true);
});
},

View File

@ -45,11 +45,11 @@ Discourse.User = Discourse.Model.extend({
statusIcon: function() {
var desc;
if(this.get('admin')) {
desc = Em.String.i18n('user.admin', {user: this.get("name")});
desc = Em.String.i18n('user.admin', {user: this.get("name")});
return '<i class="icon icon-trophy" title="' + desc + '" alt="' + desc + '"></i>';
}
if(this.get('moderator')){
desc = Em.String.i18n('user.moderator', {user: this.get("name")});
desc = Em.String.i18n('user.moderator', {user: this.get("name")});
return '<i class="icon icon-magic" title="' + desc + '" alt="' + desc + '"></i>';
}
return null;
@ -159,11 +159,9 @@ Discourse.User = Discourse.Model.extend({
'new_topic_duration_minutes',
'external_links_in_new_tab',
'enable_quoting'),
type: 'PUT',
success: function(data) {
user.set('bio_excerpt',data.user.bio_excerpt);
}
}).then(function() {
type: 'PUT'
}).then(function(data) {
user.set('bio_excerpt',data.user.bio_excerpt);
Discourse.set('currentUser.enable_quoting', user.get('enable_quoting'));
Discourse.set('currentUser.external_links_in_new_tab', user.get('external_links_in_new_tab'));
});

View File

@ -3,7 +3,7 @@
[
{
"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"]
},
{ "path": "config" },