Skip to content

Instantly share code, notes, and snippets.

@dominiek
Created March 9, 2010 15:58
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 dominiek/326725 to your computer and use it in GitHub Desktop.
Save dominiek/326725 to your computer and use it in GitHub Desktop.
/*
* 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