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

      0  

    Default Nested data not initiated

    Nested data not initiated


    Hello

    I have a User model which has many portfolios. Looks like

    Code:
    Ext.define('Owa.model.Client', {
        extend: 'Ext.data.Model',
        alias:  'model.Client',
        config: {
            fields : [ 'id', 'name'],
            hasMany: {model: 'Portfolio', name: 'portfolios'}
        }
    });
    The Porfolio model:
    Code:
    Ext.define('Owa.model.Portfolio', {
        extend : 'Ext.data.Model',
        alias : 'model.Portfolio',
        config: {
            fields : [ 'id', 'name' ],
            belongsTo: 'Owa.model.Client'
        }
    });
    And the store (with the data):
    Code:
    var data = {
       clients: [
           { 
                   name: 'James',
                   id:     100,
                   portfolios: [
                       {
                               name: 'S001', 
                               id:     21,
                               user_id: 100
                       },
                       {
                               name: 'S002', 
                               id:     22,
                               user_id: 100
                       }, 
                       {
                               name: 'S003', 
                               id:     23,
                               user_id: 100
                       }    
                   ]
           }]
    };
    
    
    Ext.define('Owa.store.Clients', { 
        //storeId: 'clients',
        //requires : ['Owa.model.Client', 'Owa.model.Portfolio'],
        extend: 'Ext.data.Store', 
        config: {
            model: 'Owa.model.Client',
            data : data,
            autoload: true,
            
            proxy: {
                type: 'memory',
                reader: {
                    type: 'json',
                    rootProperty: 'clients'
                }
            }
        }
    });
    Now, when I inspect the 'store' I can see the clients but not the HasMany association: So when I do
    Code:
    store.getAt(0).get('portfolios')
    I get undefined. I followed this doc. Any suggestions why the association isn't working ?

    Thanks a lot
    Luca

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,620
    Vote Rating
    817
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    When you use an association, the data isn't on the model. A getter method is created and should be used on the model.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

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

      0  

    Default


    thanks for the response!
    So you mean this

    Code:
    store.getAt(0).portfolios()
    This however gives

    Code:
    Uncaught TypeError: Object [object Object] has no method 'portfolios'
    I can see that store.getAt(0).$className is "Owa.model.Client" and if I inspect the Object

    Code:
    Ext.apply.create.Class
    • _data: Object
    • data: Object
      • id: 100
      • name: "James"
      • __proto__: Object
    • id: "ext-record-1"
    • internalId: 100
    • modified: Object
    • raw: Object
      • id: 100
      • name: "James"
      • portfolios: Array[3]
        • 0: Object
          • id: 21
          • name: "S001"
          • __proto__: Object
        • 1: Object
        • 2: Object
        • length: 3
        • __proto__: Array[0]
      • __proto__: Object
    • stores: Array[1]
    • __proto__: TemplateClass
    Any other suggestions ?

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

      1  

    Default


    found the problem. It was on the line of the hasMany. The model referenced needed the full path!

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Location
    Haarlem, Netherlands
    Posts
    1,243
    Vote Rating
    9
    TommyMaintz will become famous soon enough TommyMaintz will become famous soon enough

      0  

    Default


    Glad to see you have found it In general its always best to use full class names as this will cause you the least confusion. Only in stores, models, views and controllers configuration in your Application definition when using our MVC package will it automatically prepend your application name space for you automatically.

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

      0  

    Default


    ok, good to know! Thnx

  7. #7
    Sencha User
    Join Date
    Feb 2012
    Posts
    3
    Vote Rating
    0
    caroparo is on a distinguished road

      0  

    Default


    Hi, I'm doing exactly the same thing (inlined data, nested model, memory proxy, json reader) using touch2-b3. However in my case (I dumped from the store's load event handler):
    - store.first().data is an Object with the right fields and the hasMany subarray Array[0] named 'entries'
    - store.first().entries is undefined
    - I see Array[3] as defined in store.first().data.raw Object too.

    I guess it's hard to tell without the actual code, but I'm wondering if anyone's seen similar?

    Update:
    - Adding associationKey: 'entries' gets me the Array[3] into store.first().data instead of Array[0].
    - store.first().get('entries') gives me the Array[3]

  8. #8
    Sencha User
    Join Date
    Jan 2012
    Posts
    28
    Vote Rating
    2
    ratmat2000 is on a distinguished road

      0  

    Default


    Thank you for this answer ! I spend hours trying to figure out why nested data wasn't loading. The associationKey was the issue!