DEV: allow Discourse.ajax to be aborted if needed

This commit is contained in:
Sam 2015-08-10 16:12:23 +10:00
parent 066824e545
commit c2fd93ffa8

View file

@ -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;
}
});