Backbone.js / express.js options for model.save ()

I use Backbone.js on the client and node.js on the backend, and I am having problems preserving the "limited" model, as described here: http://backbonejs.org/#Model-save

As in the example, if I do

book.save({author: "Teddy"}); 

How can I access the save options using express.js, that is, the fact that I only want to save in the "author" field? I tried the following

 req.body -> gives ALL parameters of the model being saved, I want only the 'author' field req.query -> empty 

Any help is much appreciated!

+6
Jul 17 '12 at 12:19
source share
3 answers

As stated in the Model.save Documentation :

save model.save ([attributes], [options])
[...] The attribute hash (as in the set) must contain the attributes that you want to change - keys that are not mentioned will not be changed - but, the presentation of the resource is completed, it will be sent to the server.

However, you can override the save method and provide the data attribute through the parameters that will be sent to the server, rather than a complete representation of the model. For example, this will save only the attributes passed to the method:

 var M = Backbone.Model.extend({ save: function (attrs, options) { options || (options = {}); options.contentType = 'application/json'; options.data = JSON.stringify(attrs); Backbone.Model.prototype.save.call(this, attrs, options); } }); 

And the script is http://jsfiddle.net/dLFgD/




As @mikebridge commented, this behavior can now be obtained by passing the attrs option. Therefore either use

 book.save(null, { attrs: {author: "Teddy"} }); 

or save override

 var M = Backbone.Model.extend({ url: '/echo/json/', save: function(attrs, options) { options || (options = {}); options.attrs = attrs; Backbone.Model.prototype.save.call(this, attrs, options); } }); 

http://jsfiddle.net/nikoshr/dLFgD/7/




You can also send a PATCH request if you use a version of Backbone that supports it (> = 0.9.9), and your server understands this verb, as explained in @pkyeck to answer

+12
Jul 17 2018-12-12T00:
source share

with the current version of Backbone (1.1.0), you can also make a PATCH that sends only changed attributes to the server.

If instead you want the changed attributes to be sent to the server, call model.save(attrs, {patch: true}) . You will receive an HTTP PATCH request to the server with only the attributes passed.

taken here: http://backbonejs.org/#Model-save

in your case it will be:

 book.save("author", "Teddy", {patch: true}); 
+3
Feb 11 '14 at 10:08
source share

The best way to find POST options is to use the bodyParser middleware. This question is referenced by: How to get POST request parameters?

 /* On startup, register the bodyParser middleware */ app.use(express.bodyParser()); /* Then, during route execution, each parameter can be accessed with req.param */ app.post('/author/:id', function(req, res) { var author = req.param('author', null); // Second parameter is default. ... }); 
0
Jul 21 '12 at 17:00
source share



All Articles