Skip to content

Instantly share code, notes, and snippets.

@rmetzler
Created May 3, 2012 10:40
Show Gist options
  • Select an option

  • Save rmetzler/2584949 to your computer and use it in GitHub Desktop.

Select an option

Save rmetzler/2584949 to your computer and use it in GitHub Desktop.
inherited jsonp support for backbone
// 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