Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ben-ng
Created June 10, 2013 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ben-ng/5748312 to your computer and use it in GitHub Desktop.
Save ben-ng/5748312 to your computer and use it in GitHub Desktop.
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