Skip to content

Commit

Permalink
#2059 Finish ability to mock responses in API
Browse files Browse the repository at this point in the history
  • Loading branch information
jlukic committed Apr 14, 2015
1 parent e3f6d18 commit e38a168
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ $.api = $.fn.api = function(parameters) {
}

// exit conditions reached, missing url parameters
if( !url ) {
if( !url && !module.is.mocked()) {
if( module.is.form() ) {
url = $module.attr('action') || '';
module.debug('No url or action specified, defaulting to form action', url);
Expand Down Expand Up @@ -215,6 +215,9 @@ $.api = $.fn.api = function(parameters) {
form: function() {
return $module.is('form');
},
mocked: function() {
return settings.mockResponse;
},
input: function() {
return $module.is('input');
},
Expand Down Expand Up @@ -455,13 +458,12 @@ $.api = $.fn.api = function(parameters) {
},
// xhr promise
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);
return module.request.resolveWith(context, [response]);
}
Expand Down Expand Up @@ -601,7 +603,7 @@ $.api = $.fn.api = function(parameters) {
url = settings.api[action];
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);
}
}
Expand Down

0 comments on commit e38a168

Please sign in to comment.