Created
January 12, 2012 14:17
-
-
Save julien/1600764 to your computer and use it in GitHub Desktop.
BackboneMock
This file contains 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() { | |
var root = this; | |
var BackboneMock = (function() { | |
if(typeof Backbone === 'undefined') { | |
throw '"Backbone" is undefined, make sure you have loaded ' + | |
'backbone.js before using this mock utility'; | |
} | |
// Override Backbone.sync | |
// because we want to mock the requests | |
// we don't care about the method here ... | |
Backbone.sync = function(method, model, options) { | |
var resp; | |
if(urls[model.url]) { | |
resp = urls[model.url]; | |
} | |
resp ? | |
setTimeout(options.success, timeout, resp) : | |
setTimeout(options.error, timeout, 'This model\'s url is not mocked ...'); | |
}; | |
var urls = {}, | |
timeout = 1000; | |
var map = function() { | |
var args = [].slice.call(arguments, 0); | |
var i = 0, l = args.length, options; | |
for(i; i < l; i += 1) { | |
options = args[i]; | |
if(options.url) { | |
urls[options.url] = options.response || {}; | |
} | |
} | |
}; | |
return { | |
map: function() { | |
var args = [].slice.call(arguments, 0); | |
map.apply(null, args); | |
return this; | |
} | |
}; | |
}()); | |
root.BackboneMock = BackboneMock; | |
}).call(this); | |
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<script type="text/javascript" src="js/libs/jquery.js"></script> | |
<script type="text/javascript" src="js/libs/underscore.js"></script> | |
<script type="text/javascript" src="js/libs/backbone.js"></script> | |
<script type="text/javascript" src="js/backbone.mock.js"></script> | |
<script type="text/javascript"> | |
var mock = BackboneMock.map( | |
{ | |
url: '/user', | |
response: { | |
name: 'theDude', | |
age: 20, | |
email: '[email protected]' | |
} | |
} | |
); | |
// Usage with Backbone | |
var User = Backbone.Model.extend({ | |
url: '/user' | |
}); | |
var user = new User(); | |
user.fetch({ | |
error: function(model, options, response) { | |
console.log('error fetching model: ', arguments); | |
}, | |
success: function(resp, status, xhr) { | |
console.log('success fetching model: ', arguments); | |
} | |
}); | |
setTimeout(function() { | |
console.log('saving ... '); | |
user.save({url: 'http://www.thedude.com'}, { | |
error: function(model, options, reponse) { | |
console.log('error saving model data: ', arguments); | |
}, | |
success: function(resp, status, xhr) { | |
console.log('success saving model data: ', resp); | |
} | |
}); | |
}, 10000); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works with backbone 0.9.9 but doesn't with 0.9.10: here are the compatibility updates needed: https://gist.github.com/yamsellem/5195251