Created
March 9, 2010 15:58
-
-
Save dominiek/326725 to your computer and use it in GitHub Desktop.
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
/* | |
* getJSON mock mechanism, will try to fetch the URL's as flat files stored in mocks/data | |
* I'm using this with jSpec: http://visionmedia.github.com/jspec/ | |
* Dependency, SHA1 hasher: http://github.com/jed/cookie-node/blob/master/sha1.js | |
*/ | |
(function($){ | |
$.getJSON = function (url, callback) { | |
var hex = hex_sha1(url); | |
mock_path = "mocks/data/" + hex + ".json"; | |
window[hex] = function(data) { | |
console.log("=> HIT: " + mock_path); | |
callback(data); | |
}; | |
url = url.replace('=?', '='+hex); | |
console.log("Mocking request: "+url); | |
console.log("=> TRY: " + mock_path); | |
var base_url = document.location.href; | |
base_url = base_url.replace(/[^\/]+$/, ''); | |
var script = document.createElement( 'script' ); | |
script.type = 'text/javascript'; | |
script.src = base_url + mock_path; | |
$('head', document).append( script ); | |
}; | |
})(jQuery); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment