From 6b08e2d5ce9ab93a5de8b0dad6ac7bad2e9ffe14 Mon Sep 17 00:00:00 2001 From: jlukic Date: Tue, 14 Apr 2015 14:17:26 -0400 Subject: [PATCH] Add mock response async, yahoooo related to #2059 #2070 --- src/definitions/behaviors/api.js | 33 ++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/definitions/behaviors/api.js b/src/definitions/behaviors/api.js index abf58461c..de442adba 100644 --- a/src/definitions/behaviors/api.js +++ b/src/definitions/behaviors/api.js @@ -207,7 +207,6 @@ $.api = $.fn.api = function(parameters) { }, - is: { disabled: function() { return ($module.filter(settings.filter).length > 0); @@ -216,7 +215,7 @@ $.api = $.fn.api = function(parameters) { return $module.is('form'); }, mocked: function() { - return settings.mockResponse; + return (settings.mockResponse || settings.mockResponseAsync); }, input: function() { return $module.is('input'); @@ -458,14 +457,27 @@ $.api = $.fn.api = function(parameters) { }, // xhr promise xhr: function() { + var + callback + ; if( module.is.mocked() ) { - var - response = $.isFunction(settings.mockResponse) - ? settings.mockResponse.call(context, settings) - : settings.mockResponse - ; - module.verbose('Using mocked server response', response); - return module.request.resolveWith(context, [response]); + if(settings.mockResponse) { + if($.isFunction(settings.mockResponse)) { + module.verbose('Using sync mocked response callback', settings.mockResponse); + module.request.resolveWith(context, [ settings.mockResponse.call(context, settings) ]); + } + else { + module.verbose('Using mocked response', settings.mockResponse); + module.request.resolveWith(context, [ settings.mockResponse ]); + } + } + else if( $.isFunction(settings.mockResponseAsync) ) { + callback = function(response) { + module.verbose('Async callback returned response', response); + module.request.resolveWith(context, response); + }; + settings.mockResponseAsync.call(context, settings, callback); + } } else { return $.ajax(ajaxSettings) @@ -842,7 +854,8 @@ $.api.settings = { dataType : 'json', // mock response - mockResponse : false, + mockResponse : false, + mockResponseAsync : false, // callbacks before request beforeSend : function(settings) { return settings; },