Browse Source

Fix error with get xhr not returning promise, add abort method

pull/1698/merge
jlukic 9 years ago
parent
commit
42cbd74d02
1 changed files with 21 additions and 11 deletions
  1. 32
      src/definitions/behaviors/api.js

32
src/definitions/behaviors/api.js

@ -83,7 +83,8 @@ $.api = $.fn.api = function(parameters) {
.on(triggerEvent + eventNamespace, module.event.trigger)
;
}
else {
else if(settings.on == 'now') {
module.debug('Querying API now', triggerEvent);
module.query();
}
}
@ -181,17 +182,18 @@ $.api = $.fn.api = function(parameters) {
module.verbose('Creating AJAX request with settings', ajaxSettings);
if( !module.is.loading() ) {
module.request = module.create.request();
module.xhr = module.create.xhr();
}
else {
if( module.is.loading() ) {
// throttle additional requests
module.timer = setTimeout(function() {
module.request = module.create.request();
module.xhr = module.create.xhr();
}, settings.throttle);
}
else {
// immediately on first request
module.request = module.create.request();
module.xhr = module.create.xhr();
}
},
@ -421,7 +423,7 @@ $.api = $.fn.api = function(parameters) {
;
},
xhr: function() {
$.ajax(ajaxSettings)
return $.ajax(ajaxSettings)
.always(module.event.xhr.always)
.done(module.event.xhr.done)
.fail(module.event.xhr.fail)
@ -548,7 +550,7 @@ $.api = $.fn.api = function(parameters) {
var
url
;
action = action || $module.data(settings.metadata.action) || settings.action || false;
action = action || $module.data(metadata.action) || settings.action || false;
if(action) {
module.debug('Looking up url for action', action, settings.api);
if(settings.api[action] !== undefined) {
@ -563,6 +565,16 @@ $.api = $.fn.api = function(parameters) {
}
},
abort: function() {
var
xhr = module.get.xhr()
;
if( xhr && xhr.state() !== 'resolved') {
module.debug('Cancelling API request');
xhr.abort();
}
},
// reset state
reset: function() {
module.remove.error();
@ -827,9 +839,7 @@ $.api.settings = {
},
metadata: {
action : 'action',
request : 'request',
xhr : 'xhr'
action : 'action'
}
};

Loading…
Cancel
Save