PATCH request method in Backbone.js - backbone.js

What is the right way to perform PATCH request while saving model's attributes in Backbone.js?

As of Backbone.js v0.9.9, you can simply pass { patch: true } to save().
Read more: http://backbonejs.org/#changelog

In addition to James Cropchos answer I want add the following, because this stole some hours from me and maybe helps someone else:
If you use model.save(attributesToPatchObject,{patch: true}) like it is possible since backbone v.0.9.9 as stated in James Cropchos answer, you may wonder how to determine which attributes have changed since the last call of model.save() to pass them in as attributesToPatchObject which is the first argument from model.save() (or model.fetch() if you didn't save the model lately).
Backbone itself didn't keep track of those attributes. I thought the method model.changedAttributes() could fit, but as the backbone-doc says this method returns
a hash of only the model's attributes that have changed since the last
set, or false if there are none
So this method didn't fit for this need. After some research I found out that backbone itself didn't keep track of unsaved attributes (I know, not a brilliant finding if I had read the docs more carefully).
I found out that backbone.trackit is a backbone plugin which exactly add the needed feature to backbone, by adding the method unsavedAttributes() to the model. The docs of backbone.trackit says about this method:
Symmetric to Backbone's model.changedAttributes(), except that this returns a hash of the model's attributes that have changed since the last save, or false if there are none. Like changedAttributes, an external attributes hash can be passed in, returning the attributes in that hash which differ from the model.
It works like this:
//fetch an existing model from server
model.fetch({
success : function(model, respose, options) {
//tell backbone.trackit to track unsaved Attributes
model.startTracking();
}
});
//now some changes to the model happen
model.set("someProperty", "someValue");
/* save the model to server using the PATCH-Method
and only send the unsaved Attributes;
in this case only "someProperty" is sent
*/
model.save(model.unsavedAttributes(), {patch: true});
Since the unsavedAttributes() returns false if there are no unsaved Attributes, you could additionally wrap your save() statement within an if-condition which checks if unsavedAttributes() returns something other then false and only do your PATCH-Request if it's needed (because something changed).
NOTE: You didn't have to call fetch() to use startTracking() so you can use this method even with newly created models (model.isNew() returns true on that model), if there is a usecase for that.
Hopes this may save someone a little bit of research time.

You'll have to override Backbone.sync and extend the existing method mapper
var methodMap = {
'create': 'POST',
'update': 'PUT',
'delete': 'DELETE',
'read': 'GET',
'patch': 'PATCH'
};
you'll have to create your own patch method on a model like
Backbone.Model.prototype.patch = function(options)
{
// some code here that checks what attributes have changed since last save
var xhr = (this.sync || Backbone.sync).call(this, 'patch', this, options);
return xhr;
}
I'm sure you can extend Backbone further to include OPTIONS and HEAD if you needed to
Note though, that even through jQuery supports the PATCH, OPTIONS and HEAD methods, your end-users' browser may not.

Related

Backbone model urlRoot share the middle part of api

I want to fetch an api and the format is like apps/[:id]/result.
For most of the example or documentation about setting the urlRoot is based on having the same root url.
But my problem is the api share the same root and the end of the api name.
I try to use below code, but it doesn't work.
var Model = Backbone.Model.extend({
urlRoot: "/apps"
})
var model = new Model({id: 123123 + "/content"]}) //123123 just fake id
Does it any way to change only the middle of the id?
Or in this situation, use urlRoot is inappropriate?
Backbone's persistence methods, which are built around url and urlRoot, are designed for RESTful persistence. Each method is designed to communicate with a server using well-defined semantics (GET /path means list, PUT /path/:id means update, etc).
Chaning the meaning of id is a bad idea because you depart break those semantics, meaning that a) your model will not work with create, update or delete REST operations, and b) your code will become difficult to understand since id will no longer be a descriptive variable name. Backbone uses id frequently in Models and Collections, so it would be a very bad idea to change its meaning.
If you are not using a REST API, or you have a particular operation that does not fit that paradigm (like searching), it is best to implement your own methods to make custom HTTP requests.
In your case, something like this should work as you expect:
var Model = Backbone.Model.extend({
urlRoot: "/apps",
fetchResult: function() {
return $.ajax({
url: this.url() + '/result'
}).then(function(response) {
// do something with response
// return response, or the result of your processing,
// for downstream promise handlers
return response;
}, function() {
console.error('fetchResult failed');
})
},
})
With this approach the RESTful URLs work as designed and you don't lose the semantics of id.

Backbone PUT request explicitly with out using id

I'm trying to PUT the data and my model doesn't have an id.
Is it possible to explicitly tell the Save() method to PUT the data irrespective of ID.
The save method has an options parameter that can override anything on the XHR:
model.save(newVals, { type: 'PUT' })
You can also override the isNew method. PUT vs POST is determined by the result of that method. You'll also want to make sure the URL is being created correctly for new and non-new objects.
Also consider setting the idAttribute correctly so that your model does have an id field that can be used to generate a correct url. Using POST and PUT correctly (POST new items, PUT updates to items) makes your api more intuitive.

Backbone.Model.save and don't update client

I want to call save on a Backbone model and have it write data to the server, but not update the client. How do I do this?
To clarify: when Backbone saves a model, it sends all the data to the server, and then retrieves the data from the server and updates it on the client. This second step is what I want not to happen.
To clarify further: The model ('parent' model) has an attribute which is itself a model ('child' model); when it's saved to the server, this child model is converted to JSON. When the parent model updates after the save, the attribute that previously contained a reference to the child model is replaced with the parsed JSON object of the child model that was saved. This is what I need not to happen.
When the data is initially pulled from the server, the parent model "reconstitutes" that object into an appropriate child model, but this is an expensive process and there is no reason to re-do it every time save fires on the parent model, since the child model will never change.
It sounds like you do not want to parse your model when you receive the response from the server on a model.save
You can try something such as:
model.save(attributes,{
success: function() { ... },
parse : false // This will be passed to your parse function as an option
});
You would have to set-up your parse function in your corresponding model as follows:
parse: function(resp,options) {
// don't update model with the server response if {parse:false} is passed to save
if (options && !options.parse) return;
/ ... rest of your parse code ... /
Backbone currently defaults options.parse to true. Here is a short-discussion on the topic.
As discussed in that thread, perhaps you want to consider why you do not want want to update the server response to the client. There may be a cleaner way to achieve the results you desire.
Depending on how/what your server setup is, all you really have to do is issue a regular AJAX request. This is exactly what backbone does in the background so you'll just bypass the client side logic.
You could do this with native JavaScript, but I'm fairly sure you have some other library in use that can make things much easier.
For the completeness of this answer, I'll give an example with jQuery:
$.ajax({
type: "POST",
url: "http://same.as.your.model",
data: { "the" : "model" },
dataType: "JSON",
success: function(){
// once the request has returned
}
});
The $.ajax function also has some additional functionality, and you can read about it in the jQuery docs.
On client you mean Views? If you want to save your model but not render your views which happens since save will trigger a change event, you should call save with option silent:true, or set a custom option like dontchange:true when calling save and check it in when handling change. I prefer the custom option, because silent has side effects (at least in my version of backbone 1.0.0)
a little code:
when you save:
model.save({},{dontchange: true});
you install your event listeners in the view:
this.listenTo(model, 'change', function(model, options){
if (options.dontchange)
return;
this.render();
});
I ran into same problem. model.save(attrs,{patch:true, parse:false}) really did not invoke parse method but model was still merged with server response.
It is not elegant, but this worked for me:
model.clone().save(attrs,{patch:true})
I believe it's best to avoid this situation by clean REST api design.

Backbone: restore model's attributes value after a failed save with patch:true

I have a big doubt about my implementation of a model's save via form in backbone using the patch HTTP method.
My problem is that if the server return an error, at the next save backbone will send an empty object as data because the values are already been set on the model and so they are not changing again.
My code looks like that:
var data = $('#form').serialize();
this.model.set(data);
var attr = (this.model.hasChanged()) ? this.model.changedAttributes() : {};
this.model.save(attr,{patch: true, wait: true, error: function(model, xhr, options){
model.set(model.previousAttributes(),{silent: true});
}});
I've managed to "resolve" this by re-setting the attributes of the model at their previous values but it don't feel right to me... Is there a better way to manage this case? If possible I would avoid to send the entire form data to the server every time! Thanks.
EDIT: a fiddle that shows the problem, thanks to Creynders.
I suppose that your code can be simplified as follows:
var data = $('#form').serialize();
this.model.save(data ,{patch: true, wait: true});
Regarding to backbone specifications, patch: true is enough to send only changed attributes on server. wait: true promotes to not set model attributes in case of request failure. So, there is no need to set attributes before saving.
As a bonus, save method suggests model validation before sending request. So, model will not be changes in case of any validation errors. And you don't need to reset previous attributes.
Hope, I understand your problem properly.

Where does information in BackBone go?

When a save, or create is tossed towards the server, the server responds with a new randomly created object. The object can be one of many different Classes, and Backbone responds to these differentiating objects and loads a relative view.
I can only seem to figure this logic out on bootstrap, as no view has been loaded yet, so I can based on what information I am randomly receiving from the server, bootstrap and navigate to that specific route.
However, I am stuck on trying to figure out how to do this when I save an object, and receive my return data.
Here's my code broken down.
The information is saved.
#model.save(#model.toJSON(),
I have a listenener waiting for this save :
constructor: (options) ->
super(options)
#model.bind 'change:verb', _.chooser, options
_.maestra_chooser is a mixin I have in a utility belt :
_.mixin
_chooser : (item) =>
console.log item
Something to note here. The variable item is unfortunately, the same #model that was just saved. No new data there.
What I'm hoping for item to be is the new variable data from the server, so that I can take that data, see what kind of data it is, and then route to the relevant view.
This is where I believe I'm also making an architecturally unsound idea. But for reasons I don't understand enough to explain.
Does anyone know where I can access the return data from the server and appropriately navigate my app to that respective route?
Additional Information
This is how I bootstrap it appropriately :
window.router = new Project.Routers.QuestionsRouter(
{
words: #{ #words.to_json.html_safe }
});
Backbone.history.start();
router.navigate("#{#words.kind_of?(Array) ? "bar" : "foo"}", {trigger: true, replace: true})
The change event is only ever going to give you the model and the value that changed...
You can pass a success callback to your save:
#model.save(#model.toJSON(), success: (model, resp) ->
# do whatever with resp
)
where resp will contain the raw response from the server and model will contain the server side state of your model.
You can also bind to your model's sync event as mentioned in the comments:
#model.bind 'sync', _.masetra_chooser, options
the sync callback is called with arguments: model, resp and options where options is the set of options passed to save.
https://github.com/documentcloud/backbone/blob/9a12b7640f07839134e979b66df658b70e6e4fe9/backbone.js#L383
Not really sure why you are expecting to get data back from a save that'll change your page though. Seems a bit odd.
What type of data are you expecting to receive after a save that wouldn't be in your model?

Resources