konr
konr

Reputation: 2565

CRUD with just GETs and POSTs

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

Answers (1)

mu is too short
mu is too short

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 fake PUT and DELETE requests with a HTTP POST, setting the X-HTTP-Method-Override header with the true method. If emulateJSON 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

Related Questions