Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A short example of integrating Backbone.js with Geddy.js
/*
* Extend your own models from BaseModel from now on
* e.g. `MyApp.Models.Zooby = BaseModel.extend({etc:etc})`;
*
* TODO: Don't forget to override `parse()` in your model.
* It should be something like `return data.modelName;`
* TODO: Don't forget to handle errors! Check data.errors for them.
*/
var BaseModel = Backbone.Model.extend({
methodUrl: function(method) {
if(method == "delete"){
return this.urlRoot + "/" +this.attributes.id+".json";
}
else if(method == "update"){
return this.urlRoot + "/" +this.attributes.id+".json";
}
else if(method == "read"){
return this.urlRoot + "/" +this.attributes.id+".json";
}
else if(method == "create"){
return this.urlRoot + ".json";
}
return false;
}
});
/*
* Include after Backbone.js to modify Backbone.sync()
*/
(function() {
var proxiedSync = Backbone.sync;
Backbone.sync = function(method, model, options) {
options || (options = {});
//Uses our methodUrl method to get the correct path for the request
if (model.methodUrl) {
options.url = model.methodUrl(method.toLowerCase());
}
return proxiedSync(method, model, options);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.