My back-end has two separate pages, one for handling the model save request and the other for model fetch.
What is the best approach for calling save() and fetch()
I want to clearify answer of @gingerhendrix: you can pass url to to options in save and it will go directly to xhr request (it is not obvious from documentation or source code or from answer so I post ot as separate answer):
model.save({}, {url: '/custom-url'});
Gingerhendrix's answer covers the bases, but I thought it was worth elaborating on the method for passing in an options value for save/delete/fetch.
Instead of littering your code with urls every place you call one of those methods, you can also override the method on your model than then delegate back to the original Backbone.Model method like this:
var MyModel = Backbone.Model.extend({
save: function(attributes, options) {
options = _.defaults((options || {}), {url: "http://your.save.url.com/"});
return Backbone.Model.prototype.save.call(this, attributes, options);
},
// same thing for fetch and delete to give them different urls...
}
Then, you can call the method in your code without having to worry about remembering to set the url in the options.
If you have a model and a collection like :-
MyModel = Backbone.Model.extend({
url: function(){ "API/"
return "API/MyModel/" +this.get("id");
}
});
MyCollection = Backbone.Collection.extend({
model: MyModel ,
url: "API/MyModels"
});
to fetch the collection just call
MyCollection.fetch({
success: function(){
//do something here
},
error: function(){
//Handle your error
}
});
To save your model assuming you have the id of the model and you have instantiated your collection (calling it myCollection).
var model = myCollection .get(id);
model.save(
model.attributes,
{
success: function (model, response) {
//do something on success
},
error: function (model, response) {
//handle the error
}
}
);
If you're reading the source you probably already have a working solution. You essentially have two options (probably more)-
save() takes two parameters attr and options attr - is a hash of model attributes and is used to update the model before save. eg.
myModel.save(attrs)
is equivalent to
myModel.set(attrs)
myModel.save()
The second parameter is an options hash, which is passed down to this.sync() (and then Backbone.sync and then $.ajax) - setting the url in this hash will work as expected. You can pass false, undefined, or {} as the first parameter to skip the update.
Rather than have url's scattered throughout your code every time you call save() or fetch() write your own sync function to compute the url for you, the delegate to the original Backbone.sync to do the heavy lifting
eg. (This sync function adds /save to the url on CREATE, UPDATE and DELETE actions)
function mySyncFunction(method, model, options){
if(method=='GET'){
options.url = model.url;
}else{
options.url = model.url + '/save';
}
return Backbone.sync(method, model, options);
}
To use your custom sync method just declare it as part of your model
var myModel = Backbone.Model.extend({
...
"sync": mySyncFunction,
...
});