Results 1 to 4 of 4

Thread: Grid column -> Model association (belongsTo - one to many)

  1. #1

    Default Grid column -> Model association (belongsTo - one to many)

    Hey,

    i got a problem and i just want to know if i am using the association in my model correct:

    owner model
    Code:
    Ext.define('Admin.model.Relation', {
       extend: 'Ext.data.Model', 
        
        fields: [{
            name: 'relation_id',
            type: 'int'
        },{
            name: 'relation_item_type',
            type: 'string'
        },{
            name: 'relation_item',
            type: 'int'
        },{
            name: 'relation_meta',
            type: 'int'
        }],
        
        idProperty: 'relation_id',
        
        associations: [{
            type: 'belongsTo',
            model: 'Admin.model.Meta',
            primaryKey: 'meta_id',
            foreignKey: 'relation_meta',
            getterName: 'getMeta'
        }],
    
    
        proxy: {
            type: 'ajax',
            api: {
                create: 'relations/create', 
                read: 'relations/read',
                update: 'relations/update',
                destroy: 'relations/destroy',
            },
            reader: {
                type: 'json',
                root: 'items',
                totalProperty: 'total',
                successProperty: 'success',
            },
            writer: {
                type: 'json',
                writeAllFields: true,
                encode: true,
                root: 'items'
            }
        }
    });
    associated model
    Code:
    Ext.define('Admin.model.Meta', {
        extend: 'Ext.data.Model',
        
        fields: [{
            name: 'meta_id',
            type: 'int'
        },{
            name: 'meta_type',
            type: 'string'
        },{
            name: 'meta_value',
            type: 'string'
        },{
            name: 'meta_item_type',
            type: 'string'
        },{
            name: 'meta_type_text',
            type: 'string'
        }],
        
        idProperty: 'meta_id',
        
        proxy: {
            type: 'ajax',
            api: {
                create: 'metas/create', 
                read: 'metas/read',
                update: 'metas/update',
                destroy: 'metas/destroy',
            },
            reader: {
                type: 'json',
                root: 'items',
                totalProperty: 'total',
                successProperty: 'success'
            },
            writer: {
                type: 'json',
                writeAllFields: true,
                encode: true,
                root: 'items'
            }
        }
    });
    grid
    Code:
    Ext.define('Admin.view.relation.Grid' ,{
        extend: 'Ext.grid.Panel',
        alias : 'widget.relation-grid',
        id: 'relationGrid',
        store: 'Relations',
        
        initComponent: function() {
            Ext.apply(this, {
                border: false,
                dockedItems: [{
                    xtype: 'toolbar',
                    items: [{
                        iconCls: 'fam add',
                        itemId: 'add',
                        text: 'Relation hinzufgen',
                        action: 'add'
                    }]
                },{
                    xtype: 'pagingtoolbar',
                    dock: 'bottom',
                    store: 'Relations',
                    displayInfo: true
                }],
    
    
                columns: [{
                    header: 'ID',
                    hidden: true,
                    dataIndex: 'relation_id',
                    flex: 0
                },{
                    header: 'Titel',
                    flex: 2,
                    dataIndex: 'relation_meta',
                    renderer: function (value, metaData, record, row, col, store, gridView) {
                        record.getMeta({
                            success: function(profile, operation) {
                                console.log(profile.get('meta_value'));
                                return profile.get('meta_value');
                            } 
                        });
                    },
                }]
            });
            
            this.callParent(arguments);
        }
    });
    I am calling the getter in the column renderer function and i can see also an request in my network tab: .../metas/read?_dc=1365714150006&id=118

    But why id=118? Shouldn't it be meta_id? So far no problem, i am also returning 1 item.

    In the console i can see the correct value via "console.log" in the renderer function, but the grid column is empty.

    Is someone able to help me?

    Thanks!

  2. #2

    Default

    Push, sorry guys i really need help. It wont work

  3. #3

    Default



    Is it not possible to show those data in a grid?

  4. #4

    Default

    The only way I found out it works is to use mapped fields in your parent model.

    Code:
    fields: ['id', 
        {
            name: 'relatedValue',
            mapping: 'meta.metaValue'
        }
    Find more on this topic here: http://docs.sencha.com/extjs/4.2.0/#...ld-cfg-mapping

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
  •