Model synchronization with the Backbone database

2

I ask for your help to be told if it is possible to make a GET petition of a single model by making a set of one or more attributes of the model; I explain myself better:

This is a query that returns a Collection of many models when I do .fetch() , but what I want is to filter a single model if I need to order all the models and then search for it

{
 "ciudadDepartamento": "Bogotá",
 "estadoDepartamento": true,
 "fechaCreacion": "2016-05-12T00:00:00-05:00",
 "id": 1,
 "idFacultad": {
                "ciudadFacultad": "Bogotá",
                "estadoFacultad": true,
                "fechaCreacion": "2016-05-12T00:00:00-05:00",
                "id": 2,
                "nombreFacultad": "Facultad de Ingeniería y Ciencias Básicas"
               },
 "nombreDepartamento": "Ciencias básicas"
}

I have read the documentation and it says that if I want to bring all the collections I should do GET /books/ .... collection.fetch(); and for the GET /books/1 ... model.fetch(); models, but it does not work for me; What I am doing is:

var modelo = Backbone.Model.extend({"id": 1});

but it returns all the example models:

modelo.toJSON(); 
0: Object
1: Object
2: Object
3: Object
4: Object
id:1
_proto_
    
asked by Gdaimon 16.05.2016 в 21:30
source

1 answer

1

If you have this same doubt, what should be done is the following: Backbone models have an attribute that defines the id of a model with which to perform the synchronization, this attribute is idAttribute , this attribute is defined in the example model constructor

var modelo = Backbone.Model.extend({
             urlRoot: 'Endpoint',
             idAttribute: "id"
});  

After this we can do set to the created model and then the fetch() to synchronize with API like this:

modelo.set({id:1});
modelo.fetch();

This way, this model will be synchronized with an object in the database that has that id.

    
answered by 24.05.2016 в 21:28