Created
May 3, 2012 10:40
-
-
Save rmetzler/2584949 to your computer and use it in GitHub Desktop.
inherited jsonp support for backbone
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
| // here is a simple example of overriding the base backbone collection class to enable default jsonp support | |
| // i am using the github api as an example. you can obviously modify (or not) the url to suit your needs | |
| var username = 'catshirt'; | |
| Backbone.Collection = Backbone.Collection.extend({ | |
| sync: function(method, model, options) { | |
| options.dataType = 'jsonp'; | |
| options.url = _.sprintf('https://api.github.com%s', this.url); | |
| return Backbone.sync(method, model, options); | |
| }, | |
| parse: function(resp, xhr) { | |
| return resp.data; | |
| } | |
| }); | |
| var News = Backbone.Collection.extend({ | |
| url: _.sprintf('/users/%s/received_events/public', username) | |
| }); | |
| (new News()).fetch({ success: function(news) { | |
| console.log(_.sprintf('%s\'s news', username), news); | |
| }}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment