For those of you that wonder how to do it, here is how I acheived to cancel a jquery ajax request.
First, I defined a new method in my Application store that will call cancelQuery on my custom RESTAdapter.
App.Store = DS.Store.extend({
cancelQuery: function(type){
var adapter = this.adapterFor(this.modelFor(type).modelName);
if(typeof adapter.cancelQuery === 'function'){
adapter.cancelQuery();
}
}
});
In my custom RESTAdapter, I define that new function and override ajaxOptions like this :
App.YOURMODELAdapter = DS.RESTAdapter.extend({
jqXHRs: [],
ajaxOptions: function(url, type, hash) {
// Get default AjaxOptions
var ajaxOptions = this._super(url, type, hash);
// If the function was defined in the DS.RESTAdapter object,
// we must call it in out new beforeSend hook.
var defaultBeforeSend = function(){};
if(typeof ajaxOptions.beforeSend === 'function'){
defaultBeforeSend = ajaxOptions.beforeSend;
}
ajaxOptions.beforeSend = function(jqXHR, settings){
defaultBeforeSend(jqXHR, settings);
this.jqXHRs.push(jqXHR); // Keep the jqXHR somewhere.
var lastInsertIndex = this.jqXHRs.length - 1;
jqXHR.always(function(){
// Destroy the jqXHRs because the call is finished and
// we don't need it anymore.
this.jqXHRs.splice(lastInsertIndex,1);
});
};
return ajaxOptions;
},
// The function we call from the store.
cancelQuery: function(){
for(var i = 0; i < this.jqXHRs.length; i++){
this.jqXHRs[i].abort();
}
}
});
Now, you can just call cancelQuery
in the context of a controller.
this.store.cancelQuery('yourmodel');