From 894dc2c6fd8b1e3ad1d93726e1fabf1b1beae61c Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Fri, 26 Jul 2013 15:12:40 -0400
Subject: [PATCH] Moved Discourse.ajax into a mixin.

---
 app/assets/javascripts/discourse.js           | 67 +---------------
 .../javascripts/discourse/mixins/ajax.js      | 80 +++++++++++++++++++
 app/assets/javascripts/main_include.js        |  1 +
 3 files changed, 82 insertions(+), 66 deletions(-)
 create mode 100644 app/assets/javascripts/discourse/mixins/ajax.js

diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js
index 8652b5213..7a4fad378 100644
--- a/app/assets/javascripts/discourse.js
+++ b/app/assets/javascripts/discourse.js
@@ -7,7 +7,7 @@
   @class Discourse
   @extends Ember.Application
 **/
-Discourse = Ember.Application.createWithMixins({
+Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
   rootElement: '#main',
 
   // Whether the app has focus or not
@@ -181,71 +181,6 @@ Discourse = Ember.Application.createWithMixins({
     if(this.get('loginRequired')) { route.transitionTo('login'); }
   },
 
-  /**
-    Our own $.ajax method. Makes sure the .then method executes in an Ember runloop
-    for performance reasons. Also automatically adjusts the URL to support installs
-    in subfolders.
-
-    @method ajax
-  **/
-  ajax: function() {
-
-    var url, args;
-
-    if (arguments.length === 1) {
-      if (typeof arguments[0] === "string") {
-        url = arguments[0];
-        args = {};
-      } else {
-        args = arguments[0];
-        url = args.url;
-        delete args.url;
-      }
-    } else if (arguments.length === 2) {
-      url = arguments[0];
-      args = arguments[1];
-    }
-
-    if (args.success) {
-      console.warning("DEPRECATION: Discourse.ajax should use promises, received 'success' callback");
-    }
-    if (args.error) {
-      console.warning("DEPRECATION: Discourse.ajax should use promises, received 'error' callback");
-    }
-
-    // If we have URL_FIXTURES, load from there instead (testing)
-    var fixture = Discourse.URL_FIXTURES && Discourse.URL_FIXTURES[url];
-    if (fixture) {
-      return Ember.RSVP.resolve(fixture);
-    }
-
-    return Ember.Deferred.promise(function (promise) {
-      var oldSuccess = args.success;
-      args.success = function(xhr) {
-        Ember.run(promise, promise.resolve, xhr);
-        if (oldSuccess) oldSuccess(xhr);
-      };
-
-      var oldError = args.error;
-      args.error = function(xhr) {
-
-        // If it's a parseerror, don't reject
-        if (xhr.status === 200) return args.success(xhr);
-
-        promise.reject(xhr);
-        if (oldError) oldError(xhr);
-      };
-
-      // We default to JSON on GET. If we don't, sometimes if the server doesn't return the proper header
-      // it will not be parsed as an object.
-      if (!args.type) args.type = 'GET';
-      if ((!args.dataType) && (args.type === 'GET')) args.dataType = 'json';
-
-      $.ajax(Discourse.getURL(url), args);
-    });
-  },
-
-
   /**
     Subscribes the current user to receive message bus notifications
   **/
diff --git a/app/assets/javascripts/discourse/mixins/ajax.js b/app/assets/javascripts/discourse/mixins/ajax.js
new file mode 100644
index 000000000..bdb52bbc4
--- /dev/null
+++ b/app/assets/javascripts/discourse/mixins/ajax.js
@@ -0,0 +1,80 @@
+/**
+  This mixin provides an 'ajax' method that can be used to perform ajax requests that
+  respect Discourse paths and the run loop.
+
+  @class Discourse.Ajax
+  @extends Ember.Mixin
+  @namespace Discourse
+  @module Discourse
+**/
+Discourse.Ajax = Em.Mixin.create({
+
+  /**
+    Our own $.ajax method. Makes sure the .then method executes in an Ember runloop
+    for performance reasons. Also automatically adjusts the URL to support installs
+    in subfolders.
+
+    @method ajax
+  **/
+  ajax: function() {
+    var url, args;
+
+    if (arguments.length === 1) {
+      if (typeof arguments[0] === "string") {
+        url = arguments[0];
+        args = {};
+      } else {
+        args = arguments[0];
+        url = args.url;
+        delete args.url;
+      }
+    } else if (arguments.length === 2) {
+      url = arguments[0];
+      args = arguments[1];
+    }
+
+    if (args.success) {
+      console.warning("DEPRECATION: Discourse.ajax should use promises, received 'success' callback");
+    }
+    if (args.error) {
+      console.warning("DEPRECATION: Discourse.ajax should use promises, received 'error' callback");
+    }
+
+    // If we have URL_FIXTURES, load from there instead (testing)
+    var fixture = Discourse.URL_FIXTURES && Discourse.URL_FIXTURES[url];
+    if (fixture) {
+      return Ember.RSVP.resolve(fixture);
+    }
+
+    return Ember.Deferred.promise(function (promise) {
+      var oldSuccess = args.success;
+      args.success = function(xhr) {
+        Ember.run(promise, promise.resolve, xhr);
+        if (oldSuccess) oldSuccess(xhr);
+      };
+
+      var oldError = args.error;
+      args.error = function(xhr) {
+
+        // If it's a parseerror, don't reject
+        if (xhr.status === 200) return args.success(xhr);
+
+        promise.reject(xhr);
+        if (oldError) oldError(xhr);
+      };
+
+      // We default to JSON on GET. If we don't, sometimes if the server doesn't return the proper header
+      // it will not be parsed as an object.
+      if (!args.type) args.type = 'GET';
+      if ((!args.dataType) && (args.type === 'GET')) args.dataType = 'json';
+
+      $.ajax(Discourse.getURL(url), args);
+    });
+  }
+
+});
+
+
+
+
+
diff --git a/app/assets/javascripts/main_include.js b/app/assets/javascripts/main_include.js
index 81abd531f..06fa84621 100644
--- a/app/assets/javascripts/main_include.js
+++ b/app/assets/javascripts/main_include.js
@@ -4,6 +4,7 @@
 //= require_tree ./external
 
 //= require ./discourse/helpers/i18n_helpers
+//= require ./discourse/mixins/ajax
 //= require ./discourse
 
 // Pagedown customizations