Created
June 10, 2013 12:16
-
-
Save ben-ng/5748312 to your computer and use it in GitHub Desktop.
A short example of integrating Backbone.js with Geddy.js
This file contains 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
/* | |
* 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; | |
} | |
}); |
This file contains 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
/* | |
* 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