admin管理员组

文章数量:1291089

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

If instead, you'd only like the changed attributes to be sent to the server, call model.save(attrs, {patch: true}). You'll get an HTTP PATCH request to the server with just the passed-in attributes.

Source:

I could not find a good explanation of the way this actually works (and if it actually works!). It should send a PATCH request to the server with just the passed-in attributes, but it always sends a POST request to the server will ALL the attributes of the model. And with Firebug, I cannot see any difference when changing Backbone.emulateHTTP: Firebug always shows POST requests with the save method.

I created a test here: / Note that the url does not exist of course, but the important is 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 the option totally useless.

Why Backbone developers offer this options and what is it for? Could you show an example of its use?

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

If instead, you'd only like the changed attributes to be sent to the server, call model.save(attrs, {patch: true}). You'll get an HTTP PATCH request to the server with just the passed-in attributes.

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

I could not find a good explanation of the way this actually works (and if it actually works!). It should send a PATCH request to the server with just the passed-in attributes, but it always sends a POST request to the server will ALL the attributes of the model. And with Firebug, I cannot see any difference when changing Backbone.emulateHTTP: Firebug always shows POST requests with the save method.

I created a test here: http://jsfiddle/r9kXL/ Note that the url does not exist of course, but the important is 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 the option totally useless.

Why Backbone developers offer this options and what is it for? Could you show an example of its use?

Share Improve this question edited Mar 24, 2014 at 17:55 morten.c 3,5155 gold badges42 silver badges47 bronze badges asked Jun 28, 2013 at 17:48 taseenbtaseenb 1,4431 gold badge17 silver badges31 bronze badges 1
  • When I read about the { patch: true } option in the docs and try to figure out how it works, I ask myself how to determine which attributes I have to pass in order to send only the unsaved attributes to the server. If someone else want to now, you could read this related answer of mine. Hopes this helps someone. – morten.c Commented Mar 23, 2014 at 17:23
Add a ment  | 

1 Answer 1

Reset to default 11

This happens because your model isNew and Backbone "creates new instance" (method create) instead of patching existing one (method patch). Take a look - http://jsfiddle/r9kXL/1/

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

本文标签: javascriptHow to use Backbonejs partial update (patch true)Stack Overflow