I’m now a front-end developer, and I have a project which is fine to use BackboneJS, and the server-side is written by others. Is there anyone who can tell me how to override delete, update, add, and so on in a not-RESTful way? The server-side’s URL may be like this:
- add:
www.domain.com/addBookById - remove:
www.domain.com/removeBookById
Thanks a lot!!
Backbone uses
Backbone.syncto manage all communication with the server. There are two important things aboutsyncfor you; first of all, it looks like this:and the second is that you can override
syncon a per-model and per-collection basis. So you can add your ownsyncimplementation to your model:If you look at
methodyou can decide which URL to use and whether you’re doing a GET, POST, … request. Themodelwill tell you what data to send to the server. You’ll want to mergeoptionsinto the$.ajaxoptions you want to use. Have a look at the standard implementation ofBackbone.sync, it is pretty straight forward and should show you what you need to do: just replace the URL handling and drop some of the features you don’t care about (such asemulateHTTPandemulateJSON).