Instead of the default sync method, with DELETEs, PUTs &c, I'll have to use GETs and POSTs to do the CRUD operations. Is there a more elegant approach to this problem than overriding save(), fetch() and other methods?
Backbone has built in support for such things through emulateHTTP:
emulateHTTP
Backbone.emulateHTTP = trueIf you want to work with a legacy web server that doesn't support Backbones's default REST/HTTP approach, you may choose to turn on
Backbone.emulateHTTP. Setting this option will fakePUTandDELETErequests with a HTTPPOST, setting theX-HTTP-Method-Overrideheader with the true method. IfemulateJSONis also on, the true method will be passed as an additional_methodparameter.Backbone.emulateHTTP = true; model.save(); // POST to "/collection/id", with "_method=PUT" + header.
So set Backbone.emulateHTTP to true and adjust your server-side code to look at the X-HTTP-Method-Override header to see what POST requests are supposed to mean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With