Skip to content

Instantly share code, notes, and snippets.

@master5o1 master5o1/MockAjax.js

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.