Hybrid View

  1. #1
    Sencha User
    Join Date
    Jun 2010
    Posts
    139
    Vote Rating
    3
    jeanluca is on a distinguished road

      0  

    Default Answered: model associations not filled with data

    Answered: model associations not filled with data


    Hi

    I've followed the example defined here, but whatever I try I cannot get the associations to contain any data.
    So, here are my stores and two models

    Code:
    Ext.define("ModelTest.store.Data", {        
            extend: "Ext.data.Store",
            config: {
                    storeId: "usersStore",
                    data : [
                            { id: 1, firstName: "Ed Spencer",
                               products: [{id: 1, name: 'THis is a comment', user_id: 1 }]
                            }],                ],
                    model: "ModelTest.model.User",
                    autoLoad: true,
            }
    });
    The User model
    Code:
    Ext.define('ModelTest.model.User', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                {name: 'id',   type: 'int'},
                {name: 'name', type: 'string'}
            ],
            hasMany: {model: 'Product', name: 'products'}
        }
    });
    and finally the Product model
    Code:
    Ext.define('ModelTest.model.Product', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                {name: 'id',      type: 'int'},
                {name: 'user_id', type: 'int'},
                {name: 'name',    type: 'string'}
            ]
        }
    });
    Then, from the console I did

    Code:
    Ext.getStore('usersStore').getData().each(function(i) {     
        console.dir(i.products());
    })
    which returns 'products is not defined'. Any suggestions why the association is empty ?

    Cheers

  2. try to use the associationKey, and on model use the complete name of the model

    Code:
    Ext.define('ModelTest.model.User', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                {name: 'id',   type: 'int'},
                {name: 'name', type: 'string'}
            ],
            hasMany: {model: 'ModelTest.model.Product', name: 'products', associationKey: 'products'}
        }
    });

  3. #2
    Sencha User
    Join Date
    Oct 2012
    Posts
    71
    Vote Rating
    2
    Answers
    4
    Tioecomp is on a distinguished road

      1  

    Default


    try to use the associationKey, and on model use the complete name of the model

    Code:
    Ext.define('ModelTest.model.User', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                {name: 'id',   type: 'int'},
                {name: 'name', type: 'string'}
            ],
            hasMany: {model: 'ModelTest.model.Product', name: 'products', associationKey: 'products'}
        }
    });

  4. #3
    Sencha User
    Join Date
    Jun 2010
    Posts
    139
    Vote Rating
    3
    jeanluca is on a distinguished road

      0  

    Default


    Yes that works! Thnx. But, what is a little bit strange, is that user.products() returns a store. This store has products, but shouldn't user.products() return a collection/array of products ?

  5. #4
    Sencha User
    Join Date
    Oct 2012
    Posts
    71
    Vote Rating
    2
    Answers
    4
    Tioecomp is on a distinguished road

      0  

    Default


    That I dont know man, I have a store returned too.

Thread Participants: 1

Tags for this Thread