Browse Source

#2059 Finish ability to mock responses in API

pull/2169/head
jlukic 9 years ago
parent
commit
e38a168448
1 changed files with 11 additions and 9 deletions
  1. 20
      src/definitions/behaviors/api.js

20
src/definitions/behaviors/api.js

@ -161,7 +161,7 @@ $.api = $.fn.api = function(parameters) {
} }
// exit conditions reached, missing url parameters // exit conditions reached, missing url parameters
if( !url ) {
if( !url && !module.is.mocked()) {
if( module.is.form() ) { if( module.is.form() ) {
url = $module.attr('action') || ''; url = $module.attr('action') || '';
module.debug('No url or action specified, defaulting to form action', url); module.debug('No url or action specified, defaulting to form action', url);
@ -215,6 +215,9 @@ $.api = $.fn.api = function(parameters) {
form: function() { form: function() {
return $module.is('form'); return $module.is('form');
}, },
mocked: function() {
return settings.mockResponse;
},
input: function() { input: function() {
return $module.is('input'); return $module.is('input');
}, },
@ -455,13 +458,12 @@ $.api = $.fn.api = function(parameters) {
}, },
// xhr promise // xhr promise
xhr: function() { xhr: function() {
if(settings.mockResponse) {
if( $.isFunction(settings.mockResponse) ) {
response = settings.mockResponse.call(context, settings);
}
else {
response = settings.mockResponse;
}
if( module.is.mocked() ) {
var
response = $.isFunction(settings.mockResponse)
? settings.mockResponse.call(context, settings)
: settings.mockResponse
;
module.verbose('Using mocked server response', response); module.verbose('Using mocked server response', response);
return module.request.resolveWith(context, [response]); return module.request.resolveWith(context, [response]);
} }
@ -601,7 +603,7 @@ $.api = $.fn.api = function(parameters) {
url = settings.api[action]; url = settings.api[action];
module.debug('Found template url', url); module.debug('Found template url', url);
} }
else if( !module.is.form() ) {
else if( !module.is.form() && !module.is.mocked() ) {
module.error(error.missingAction, settings.action, settings.api); module.error(error.missingAction, settings.action, settings.api);
} }
} }

Loading…
Cancel
Save