1.

How Backbone Decides If It Should Use Post/get/ Request To The Server? What Are The Methods Backbone Has Reserved For These Operations?

Answer»

If we instantiate a model with an id, Backbone.js will automatically perform a GET request to the urlRoot + ‘/id’ using fetch() method. (CONFORMING to RESTful conventions)

If the id attribute of the model is null, Backbone.js will send a POST request to the urlRoot of the server using save() method.

If the id attribute of the model is not null, Backbone.js will send a PUT request instead of a POST request using save() method.

If a model has an id we know that it exists on the server, so if we wish to remove it from the server we can call DESTROY() method. destroy will fire off a DELETE /user/id (conforming to RESTful conventions).

See the example below for all these above operations:

// Here we have set only the `id` of the model so it will call fetch() method.
var user = new Usermodel({id: 1});

// The fetch below will perform GET /user/1
// The server should RETURN the id, name and email from the database
user.fetch({
success: function (user) {
alert(user.toJSON());
}
})

// Here we have set all the attributes, along with id, 
// of the model so it will call save() method with PUT
var user = new Usermodel({
id: 1,
name: 'Thomas',
email: 'myemailid@domain.com'
});

// Let's CHANGE the name and update the server
// Because there is `id` present, Backbone.js will fire
// PUT /user/1 with a payload of `{name: 'Davis', email: 'myemailid@domain.com'}`
user.save({name: 'Davis'}, {
success: function (model) {
alert(user.toJSON());
}
});

// Here we have set all the properties of model.
var user = new Usermodel({
id: 1,
name: 'Thomas',
email: 'thomasalwyndavis@gmail.com'
});

// Because there is `id` present, Backbone.js will fire
// DELETE /user/1 
user.destroy({
success: function () {
alert('Destroyed');
}
});

If we instantiate a model with an id, Backbone.js will automatically perform a GET request to the urlRoot + ‘/id’ using fetch() method. (conforming to RESTful conventions)

If the id attribute of the model is null, Backbone.js will send a POST request to the urlRoot of the server using save() method.

If the id attribute of the model is not null, Backbone.js will send a PUT request instead of a POST request using save() method.

If a model has an id we know that it exists on the server, so if we wish to remove it from the server we can call destroy() method. destroy will fire off a DELETE /user/id (conforming to RESTful conventions).

See the example below for all these above operations:

// Here we have set only the `id` of the model so it will call fetch() method.
var user = new Usermodel({id: 1});

// The fetch below will perform GET /user/1
// The server should return the id, name and email from the database
user.fetch({
success: function (user) {
alert(user.toJSON());
}
})

// Here we have set all the attributes, along with id, 
// of the model so it will call save() method with PUT
var user = new Usermodel({
id: 1,
name: 'Thomas',
email: 'myemailid@domain.com'
});

// Let's change the name and update the server
// Because there is `id` present, Backbone.js will fire
// PUT /user/1 with a payload of `{name: 'Davis', email: 'myemailid@domain.com'}`
user.save({name: 'Davis'}, {
success: function (model) {
alert(user.toJSON());
}
});

// Here we have set all the properties of model.
var user = new Usermodel({
id: 1,
name: 'Thomas',
email: 'thomasalwyndavis@gmail.com'
});

// Because there is `id` present, Backbone.js will fire
// DELETE /user/1 
user.destroy({
success: function () {
alert('Destroyed');
}
});



Discussion

No Comment Found