How to use Backbone.js partial update (patch: true)?

In saving the method of the Model chapter, the Backbone.js documentation says:

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.

Source: http://backbonejs.org/#Model-save

I could not find a good explanation of how this works (and if it really works!). It should send a PATCH request to the server with only the attributes passed , but it always sends a POST request to the server ALL model attributes . And with Firebug, I don’t see any difference when changing Backbone.emulateHTTP : Firebug always shows POST requests using the save method.

I created a test here: http://jsfiddle.net/r9kXL/ Note that the URL does not exist, but it is important to see the POST request in Firebug. As you can see, if you try to send only one attribute, it will always send everything to the server, making this parameter completely useless.

Why are Backbone developers offering these options and what are they for? Could you show an example of its use?

+4
source share
1 answer

This is because your isNew and Backbone model “creates a new instance” ( create method) instead of fixing the existing one ( patch method). Take a look - http://jsfiddle.net/r9kXL/1/

 'create': 'POST', 'update': 'PUT', 'patch': 'PATCH', 'delete': 'DELETE', 'read': 'GET' 
+11
source

Source: https://habr.com/ru/post/1488738/


All Articles