| 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. 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. |
|