From 6cbcd6e4a661f4bf29ba5fbb2abe07538b453855 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 7 May 2013 10:58:41 -0400 Subject: [PATCH] The last of the callback style AJAX have been converted to promises. --- .../discourse/components/message_bus.js | 2 +- .../javascripts/discourse/models/category.js | 2 +- .../javascripts/discourse/models/model.js | 15 ------------- .../javascripts/discourse/models/post.js | 21 +++++++++--------- .../javascripts/discourse/models/topic.js | 22 ++++++++++--------- .../javascripts/discourse/models/user.js | 12 +++++----- sublime-project | 2 +- 7 files changed, 31 insertions(+), 45 deletions(-) diff --git a/app/assets/javascripts/discourse/components/message_bus.js b/app/assets/javascripts/discourse/components/message_bus.js index 1c19de3686e..e0d48979397 100644 --- a/app/assets/javascripts/discourse/components/message_bus.js +++ b/app/assets/javascripts/discourse/components/message_bus.js @@ -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', diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index 9087b172861..d9ea2925e5c 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -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'), diff --git a/app/assets/javascripts/discourse/models/model.js b/app/assets/javascripts/discourse/models/model.js index d06c30d6ee5..4eb3ae8bfe8 100644 --- a/app/assets/javascripts/discourse/models/model.js +++ b/app/assets/javascripts/discourse/models/model.js @@ -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 diff --git a/app/assets/javascripts/discourse/models/post.js b/app/assets/javascripts/discourse/models/post.js index cfa7faa47f9..234932f8f97 100755 --- a/app/assets/javascripts/discourse/models/post.js +++ b/app/assets/javascripts/discourse/models/post.js @@ -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; diff --git a/app/assets/javascripts/discourse/models/topic.js b/app/assets/javascripts/discourse/models/topic.js index e6529bd303f..e96b5cf5f93 100755 --- a/app/assets/javascripts/discourse/models/topic.js +++ b/app/assets/javascripts/discourse/models/topic.js @@ -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); }); }, diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index 76d269d35cb..35434b24f3c 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -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 ''; } 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 ''; } 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')); }); diff --git a/sublime-project b/sublime-project index a2735e98e65..1c37118bdfa 100644 --- a/sublime-project +++ b/sublime-project @@ -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" },