HTTP PUT is for in place entire entity updates. HTTP PATCH is for partial updates.
This isn't some opinion these guys came up with. It's HTTP standards.
Also if you don't like what save does. Write your own Backbone.sync and be done with it. Still super flexible.
That's not intended to be a counterpoint. In the wild, many companies don't use Rails. :)
With that said, it looks like many do already support it (from the same article you linked): Apache, nginx, Phusion Passenger, Unicorn, Thin, and WEBrick.
1. You can pass any arguments to the ajax call in the options hash passed to the various calls:
Foo = Backbone.Model.extend({url: "/bar" })
f = new Foo
f.fetch({type: "BLAH"})
leads to a "BLAH /bar" request instead of a GET.
2. You can override Backbone.sync to do an application-wide override. The basic implementation is fairly simple as a starting point and/or you can just wrap it.3. You can override the sync methods of Backbone.Model or Backbone.Collection. By default these just proxy to Backbone.sync
4. You can extend either of them and derive your own models and collections from your extended version.
5. You can replace .sync for any _specific_ model or collection object if you have specific sync needs for specific subsets of your application.
Given this, I really don't think cluttering the default code with more options is worthwhile - any other variations would be better provided for by providing an alternate implementation which can optionally proxy to the default/original for cases that doesn't need special handling.
This is part of what I like about Backbone - it is small and focused and sufficiently decoupled that replacing parts of the guts of it is simple to do.
> PUT as a default makes sense but it should be able to support
> POST, PATCH, GET, CHANGETHETHING or anything else, as browsers
> now support arbitrary method strings in XHR.
... it does (as mentioned by vidarh). The full suite of jQuery options are passed through transparently. What this particular feature means is that: model.save()
// PUT all of the models current attributes to model.url
model.save(attrs, {patch: true})
// PATCH just the `attrs` to model.url // Override Backbone.sync to use the PUT HTTP method for PATCH requests
// when doing Model#save({...}, { patch: true });
var originalSync = Backbone.sync;
Backbone.sync = function(method, model, options) {
if (method === 'patch') options.type = 'PUT';
return originalSync(method, model, options);
};
This feels a little off to me. The nice thing about Backbone vs. other frameworks is that it's not overly opinionated, you can structure your backend any way you want, so I'm not sure why Backbone has an opinion of what http methods save() uses. PUT as a default makes sense but it should be able to support POST, PATCH, GET, CHANGETHETHING or anything else, as browsers now support arbitrary method strings in XHR. Perhaps an override some where?