Results 1 to 2 of 2

Thread: Belongs to -> Empty data Ext 4.2 MVC

  1. #1
    Sencha User
    Join Date
    Oct 2010
    Location
    Cusco
    Posts
    11
    Vote Rating
    0
      0  

    Default Unanswered: Belongs to -> Empty data Ext 4.2 MVC

    Iím trying to show parent record data in my grid using ďbelongs to ďrelation in MVC, but getter object for record is empty (it shows only raw data).


    Is it necessary to load parent modelís store? Chidrenís json data contains all data I need, and loading all parents store would increase traffic for the application.


    I set it to auto load but it is the same.


    When autoload set to false parentís store is empty, it is only part of the record in the childrenís store, where related (parent) data is supposed to be stored?,


    Do I need to use setter function to persist raw data to the store?


    Parent's Model:
    Code:
    Ext.define('MyApp.model.FfileModel', {
        extend: 'Ext.data.Model',
    
    
        uses: [
            'MyApp.model.FservicioModel'
        ],
    
    
        fields: [
            {
                name: 'id',
                type: 'int'
            },{
                name: 'nombre',
                type: 'string'
            },{
                name: 'observaciones',
                type: 'string'
            },
    ],
    
    
        hasMany: {
            associationKey: 'fservicios',
            model: 'MyApp.model.FservicioModel',
            autoLoad: false,
            foreignKey: 'ffile_id'
        }
    });



    Children's model:
    Code:
    Ext.define('MyApp.model.FservicioModel', {
        extend: 'Ext.data.Model',
    
    
        uses: [
            'MyApp.model.FfileModel'
        ],
    
    
        fields: [
            {
                name: 'id',
                type: 'int'
            }, {
                name: 'ffile_id',
                type: 'int'
            },
    ],
    
    
        belongsTo: {
            associationKey: 'ffile',
            model: 'MyApp.model.FfileModel',
            foreignKey: 'ffile_id',
            getterName: 'getFfile',
            setterName: 'setFfile'
        }
    });

    Children's json
    Code:
    { servicios:[
        {
        "id": 1,
        "ffile_id":1,
        "ffile":[
            {
            id:1,
            nombre:"xxx",
            observaciones:"xxx"
            }
            ]
        },
        {"id": 2,
          "ffile_id":1,
           "ffile":[
            {
            id:1,
            nombre:"xxx",
            observaciones:"xxx"
            }
            ]
        },
    ]}

    Renderer;
    Code:
    renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
        return record.getFfile().get('nombre');
    }

    Content of getter object:
    Code:
    ({raw:[{id:1, nombre:"xxx", observaciones:"xxx"}], modified:{}, data:{nombre:"",  observaciones:"" }, hasListeners:{}, events:{}, stores:[], phantom:false, internalId:"ext-record-1", id:"MyApp.model.FfileModel-ext-record-1"})

  2. #2
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    6,154
    Answers
    501
    Vote Rating
    247
      0  

    Default

    Unless you require the extended functionality of model associations you may very well be able to get away with a much smaller code footprint. See the example below:

    Client code
    Code:
    Ext.define('MyApp.model.FfileModel', {
        extend: 'Ext.data.Model',
        fields: ['ffile_id', 'ffile', 'id'],
        proxy: {
            type: 'ajax',
            url: 'data/json.json',
            reader: {
                type: 'json',
                root: 'servicios'
            }
        }
    });
    
    
    
    
    var store = Ext.create('Ext.data.Store', {
        model: 'MyApp.model.FfileModel',
        autoLoad: true
    });
    
    
    Ext.widget('gridpanel', {
        renderTo: document.body,
        height: 200,
        width: 200,
        store: store,
        columns: [{
            text: 'ffile_id',
            dataIndex: 'ffile_id'
        }, {
            text: 'nombre',
            dataIndex: 'ffile',
            renderer: function (val, meta, rec) {
                return rec.get('ffile')[0].nombre;
            }
        }]
    });
    Server response:
    Code:
    {
        servicios: [{
            "id": 1,
            "ffile_id": 1,
            "ffile": [{
                id: 1,
                nombre: "xxx",
                observaciones: "xxx"
            }]
        }, {
            "id": 2,
            "ffile_id": 1,
            "ffile": [{
                id: 1,
                nombre: "xxx",
                observaciones: "xxx"
            }]
        }]
    }
    Are you a Sencha products veteran who has wondered what it might be like to work at Sencha? If so, please reach out to our human resources manager: fabienne.bell@sencha.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •