From c2fd93ffa8b9498dc9c6c4269a6289111fc585f1 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 10 Aug 2015 16:12:23 +1000 Subject: [PATCH] DEV: allow Discourse.ajax to be aborted if needed --- .../javascripts/discourse/mixins/ajax.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/mixins/ajax.js b/app/assets/javascripts/discourse/mixins/ajax.js index 6817150d5..4ca8c9a13 100644 --- a/app/assets/javascripts/discourse/mixins/ajax.js +++ b/app/assets/javascripts/discourse/mixins/ajax.js @@ -19,6 +19,7 @@ Discourse.Ajax = Em.Mixin.create({ **/ ajax: function() { var url, args; + var ajax; if (arguments.length === 1) { if (typeof arguments[0] === "string") { @@ -95,22 +96,32 @@ Discourse.Ajax = Em.Mixin.create({ args.cache = false; } - $.ajax(Discourse.getURL(url), args); + ajax = $.ajax(Discourse.getURL(url), args); }; + var promise; + // For cached pages we strip out CSRF tokens, need to round trip to server prior to sending the // request (bypass for GET, not needed) if(args.type && args.type.toUpperCase() !== 'GET' && !Discourse.Session.currentProp('csrfToken')){ - return new Ember.RSVP.Promise(function(resolve, reject){ - $.ajax(Discourse.getURL('/session/csrf'), {cache: false}) + promise = new Ember.RSVP.Promise(function(resolve, reject){ + ajax = $.ajax(Discourse.getURL('/session/csrf'), {cache: false}) .success(function(result){ Discourse.Session.currentProp('csrfToken', result.csrf); performAjax(resolve, reject); }); }); } else { - return new Ember.RSVP.Promise(performAjax); + promise = new Ember.RSVP.Promise(performAjax); } + + promise.abort = function(){ + if (ajax) { + ajax.abort(); + } + }; + + return promise; } });