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) .on(triggerEvent + eventNamespace, module.event.trigger)
; ;
} }
else {
else if(settings.on == 'now') {
module.debug('Querying API now', triggerEvent);
module.query(); module.query();
} }
} }
@ -181,17 +182,18 @@ $.api = $.fn.api = function(parameters) {
module.verbose('Creating AJAX request with settings', ajaxSettings); 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 // throttle additional requests
module.timer = setTimeout(function() { module.timer = setTimeout(function() {
module.request = module.create.request(); module.request = module.create.request();
module.xhr = module.create.xhr(); module.xhr = module.create.xhr();
}, settings.throttle); }, 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() { xhr: function() {
$.ajax(ajaxSettings)
return $.ajax(ajaxSettings)
.always(module.event.xhr.always) .always(module.event.xhr.always)
.done(module.event.xhr.done) .done(module.event.xhr.done)
.fail(module.event.xhr.fail) .fail(module.event.xhr.fail)
@ -548,7 +550,7 @@ $.api = $.fn.api = function(parameters) {
var var
url url
; ;
action = action || $module.data(settings.metadata.action) || settings.action || false;
action = action || $module.data(metadata.action) || settings.action || false;
if(action) { if(action) {
module.debug('Looking up url for action', action, settings.api); module.debug('Looking up url for action', action, settings.api);
if(settings.api[action] !== undefined) { 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 state
reset: function() { reset: function() {
module.remove.error(); module.remove.error();
@ -827,9 +839,7 @@ $.api.settings = {
}, },
metadata: { metadata: {
action : 'action',
request : 'request',
xhr : 'xhr'
action : 'action'
} }
}; };

Loading…
Cancel
Save