Reputation: 2565
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?
Upvotes: 3
Views: 1696
Reputation: 434595
Backbone has built in support for such things through emulateHTTP
:
emulateHTTP
Backbone.emulateHTTP = true
If 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 fakePUT
andDELETE
requests with a HTTPPOST
, setting theX-HTTP-Method-Override
header with the true method. IfemulateJSON
is also on, the true method will be passed as an additional_method
parameter.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.
Upvotes: 5