From 42cbd74d0233b70da4726f944c2ec3cf6a0fc95f Mon Sep 17 00:00:00 2001 From: jlukic Date: Tue, 20 Jan 2015 16:31:48 -0500 Subject: [PATCH] Fix error with get xhr not returning promise, add abort method --- src/definitions/behaviors/api.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/definitions/behaviors/api.js b/src/definitions/behaviors/api.js index 3d1e8a84e..b5db943db 100644 --- a/src/definitions/behaviors/api.js +++ b/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' } };