Beetlejuice
Beetlejuice

Reputation: 4425

Mapping json to extjs model

I'm using ExtJs4 and I had the json bellow. I have some problems when mapping it to Extjs Models:

    {
      "success": true,
      "data": {
        "bairro": "Vila Paulista",
        "cep": "86730-000",
        "convenio_id": 52,
        "nome": "Peter Parker",
        "consultas": [
          {
            "data": "08/08/2008",
            "paciente_id": 200,
            "senha": null
          },
          {
            "data": "22/02/2010",
            "paciente_id": 200,
            "senha": null
          },
        "convenio": {
          "id": 52,
          "nome": "C80"
        }
      }
    }

Here is my model:

Ext.define('App.model.vo.Paciente', {
    extend: 'Ext.data.Model',
    requires: [
        'App.model.vo.Consulta',
        'App.model.vo.Convenio'
    ],
    fields: [
        {name: 'id', type: 'auto'},
        {name: 'bairro', type: 'auto'},
        {name: 'cep', type: 'auto'},
        {name: 'convenio_id', type: 'auto'},
        {name: 'nome', type: 'auto'}
    ],
    belongsTo: { 
        model: 'App.model.vo.Convenio', 
        name: 'convenio', 
        foreignKey: 'convenio_id', 
        getterName: 'getConvenio'},
    hasMany: { 
        model: 'App.model.vo.Consulta', 
        name: 'consultas'},
        idProperty: 'id',
    proxy: {
        type: 'rest',
        url : '/pacientes',
        format: 'json',
        reader: {
            root: 'data',
            successProperty: 'success'
        },
        writer: {
            getRecordData: function(record) {
                return { paciente: record.data };
            }
        }
    }
});

and to load the data:

App.model.vo.Paciente.load(id, {
        success: function(rec) {
            console.log(rec.consultas());
            console.log(rec.getConvenio());
        }
    });

The rec.consultas() display all child records very well, but rec.getConvenio() make a request to the server to get the data again.

I need to use the data that is already in json file, in this case:

    "convenio": {
      "id": 52,
      "nome": "C80"
    }

thanks.

Upvotes: 1

Views: 4856

Answers (1)

Beetlejuice
Beetlejuice

Reputation: 4425

I just had to declare:

{name: 'convenio', type: 'auto'}

in my model and remove belongsTo stuff.

Upvotes: 1

Related Questions