Skip to content

Instantly share code, notes, and snippets.

@denisos
Created September 27, 2011 17:00
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save denisos/1245613 to your computer and use it in GitHub Desktop.
Save denisos/1245613 to your computer and use it in GitHub Desktop.
Backbone.js model calling jsonp
// backbone.js continues to impress, I needed to get data from a jsonp api
// I really wanted to do this the "right" backbone.js way and create a model and call fetch.
// But the default backbone synch is not jsonp
// Turns out you can override a synch on a per model basis (thanks stackoverflow)
// whats nice is backbone.js continue to work as expected with the override
// here's a snippet (some changes to protect our privacy). An improvement could be to create a jsonp model class which MyModel inherits
// the synch function is most important below, that's what tells backbone it's jsonp
MyModel = Backbone.Model.extend({
url: function() {
return '/yourJsonpUrlhere';
},
// override backbone synch to force a jsonp call
sync: function(method, model, options) {
// Default JSON-request options.
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: model.url(),
jsonp: "jsonpCallback", // the api requires the jsonp callback name to be this exact name
processData: false
}, options);
// Make the request.
return $.ajax(params);
},
parse: function(response) {
// parse can be invoked for fetch and save, in case of save it can be undefined so check before using
if (response) {
if (response.success ) {
// here you write code to parse the model data returned and return it as a js object
// of attributeName: attributeValue
return {name: response.name}; // just an example,
}
}
}
});
@Stupid21
Copy link

Hi.. I'm trying the same with struts controller as backend... What should be response from server in this case?. Should the server return something like function call ?? Any change is requiers in server side for this?

@mdigital
Copy link

mdigital commented Jun 7, 2012

Using jsonp: "jsonpCallback" stops backbone from ever calling model.parse which means this doesn't work! If you use url: model.url()+"?callback=?" and remove the jsonp option it works great.

@denisos
Copy link
Author

denisos commented Jun 11, 2012

Hi, sorry for not replying to posts sooner. Re last comment "doesn't work". Yes it did! I used this code on a previous project for a large US retailer to do just this.
I did modify and simplify code for this gist, if it's not working and my fault I can only think it was me translating incorrectly. Otherwise it should work.

@mdigital
Copy link

It may be a bug in the version of jquery? I did some debugging and using that technique the ajax success event is never fired. This event is used to trigger model.parse(), I've forked your version for others who might have a similar issue to me. Thanks for the code though, it certainly got me most of the way there.

https://gist.github.com/2886126

@cabhishek
Copy link

you can simply do this someCollection.fetch({ dataType: 'jsonp' });

@gstephens
Copy link

Thanks for these posts. I also found that mdigital's version worked for me but parse was never called in the code above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment