Skip to content

Instantly share code, notes, and snippets.

@jason-s13r
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason-s13r/2f8bc9aeabf8045e34c0 to your computer and use it in GitHub Desktop.
Save jason-s13r/2f8bc9aeabf8045e34c0 to your computer and use it in GitHub Desktop.
silly little thing.
function MockAjax() {
var self = this;
var responses = {
'http://api.example.com/cart/add.json?a=5&b=3': {
"data": 8
}
};
function request(url, success, failure) {
window.setTimeout(function () {
if (responses[url] === undefined) {
failure();
return;
}
success(responses[url]);
}, Math.random() * 1000);
}
self.mock = function(url, response) {
responses[url] = response;
};
self.get = function(url, success, failure) {
request(url, success, failure);
};
self.post = function(url, success, failure) {
request(url, success, failure);
};
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment