I’m using Backbone.js on the client and node.js on the backend, and I’m having a bit of trouble doing a ‘limited’ model save, as explained here : http://backbonejs.org/#Model-save
As in the example, if I do
book.save({author: "Teddy"});
how do I access the parameters of the save using express.js, i.e. the fact that I only want to save to the ‘author’ field? I’ve tried the following
req.body -> gives ALL parameters of the model being saved, I want only the 'author' field
req.query -> empty
Any help much appreciated!
As stated in Model.save documentation:
You can however override the save method and provide a
dataattribute via the options which will be sent to the server instead of the full model representation. For example, this will only save the attributes passed to the method :And a Fiddle http://jsfiddle.net/dLFgD/
As @mikebridge noted in the comments, this behavior can now be obtained by passing an
attrsoption. So either useor keep the override
http://jsfiddle.net/nikoshr/dLFgD/7/
You could also send a PATCH request if you’re using a Backbone version that supports it (>=0.9.9) and your server understands that verb, as explained in @pkyeck’s answer