Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.org/#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.net/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?

like image 627
taseenb Avatar asked Dec 21 '22 03:12

taseenb


1 Answers

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.net/r9kXL/1/

'create': 'POST',
'update': 'PUT',
'patch':  'PATCH',
'delete': 'DELETE',
'read':   'GET'
like image 61
Vitalii Petrychuk Avatar answered Dec 28 '22 07:12

Vitalii Petrychuk