1.

How The Actions Performed On Model Is Translated To Restful Operations? Give An Example.?

Answer»

Models are used to represent data from your server and actions you perform on them will be translated to RESTful operations. The ID attribute of a model identifies how to find it on the database usually mapping to the surrogate key.

Suppose you have a table Users with columns id, name, email and you want to save the model BACK on server when user clicks save button. If the id attribute of the model is NULL, Backbone.js will send a POST request to the urlRoot of the server. For this,

see the example below:

var UserModel = Backbone.Model.extend({
urlRoot: '/user', //RESTful API relative path
defaults: {
name: '',
email: ''
}
});
var user = NEW UserModel();
// Notice that we haven't set an `id`. In case of update operation, we need to pass 'id' as WELL.
var userDetails = {
name: 'Thomas',
email: 'youemailid@domain.com'
};
// Because we have not set a `id` the server will call
// POST /user with a payload of {name:'Thomas', email: 'youremailid@domain.com'}
// The server should save the data and return a response containing the new `id`
user.save(userDetails, {
success: function (user) {
alert(user.toJSON());
}
})

Models are used to represent data from your server and actions you perform on them will be translated to RESTful operations. The id attribute of a model identifies how to find it on the database usually mapping to the surrogate key.

Suppose you have a table Users with columns id, name, email and you want to save the model back on server when user clicks save button. If the id attribute of the model is null, Backbone.js will send a POST request to the urlRoot of the server. For this,

see the example below:

var UserModel = Backbone.Model.extend({
urlRoot: '/user', //RESTful API relative path
defaults: {
name: '',
email: ''
}
});
var user = new UserModel();
// Notice that we haven't set an `id`. In case of update operation, we need to pass 'id' as well.
var userDetails = {
name: 'Thomas',
email: 'youemailid@domain.com'
};
// Because we have not set a `id` the server will call
// POST /user with a payload of {name:'Thomas', email: 'youremailid@domain.com'}
// The server should save the data and return a response containing the new `id`
user.save(userDetails, {
success: function (user) {
alert(user.toJSON());
}
})



Discussion

No Comment Found