Last active
August 29, 2015 14:24
-
-
Save jason-s13r/2f8bc9aeabf8045e34c0 to your computer and use it in GitHub Desktop.
silly little thing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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