Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Problems with data association in models.

  1. #1
    Sencha User
    Join Date
    Jan 2012
    Posts
    74

    Default Problems with data association in models.

    I have a problem with getting nested data to appear as nested on the store as well.

    If I check the raw data on each record put in the store I can see the data, but all that is on the actual record, is an empty sfbresources object. Any help would be appreciated with getting the nested data in it's right place.

    I have some data:
    Code:
    {
        "results": [
            {
                "sfbnr": 1900,
                "sfbtext": "Bygningsbasis bygning og trren",
                "label": "1900 Bygningsbasis bygning og trren",
                "sfbresources": [
                    {
                        "sfbnr": 1003,
                        "sfbtext": "Spunsvgge",
                        "leaf": "true"
                    },
                    {
                        "sfbnr": 1004,
                        "sfbtext": "Plefundamenter og brndfundamenter",
                        "leaf": "true"
                    },
                    {
                        "sfbnr": 1301,
                        "sfbtext": "Terrndk, kldre",
                        "leaf": "true"
                    }
                ]
            }
        ]
    }
    And two models and a store:
    Code:
     Ext.define('MyApp.store.BuildingElements', {
        extend: 'Ext.data.Store',
         config: {
            model: 'MyApp.model.BuildingElement',
     
            proxy: {
                type: 'ajax',
                url: 'xhr/xhrStore.asp?cmd=sfblist',
                reader: {
                    type: 'json',
                    rootProperty: 'results'
                }
            }
        }
    });
    
    Ext.define('MyApp.model.BuildingElement', {
        extend: 'Ext.data.Model',
     
        config: {
            fields: [
                { name: 'sfbnr', type: 'int' },
                { name: 'sfbtext', type: 'string' },
                { name: 'label', type: 'string' }
            ],
            hasMany: 'MyApp.model.SfbResource'
        }
    });
    
    Ext.define('MyApp.model.SfbResource', {
        extend: 'Ext.data.Model',
     
        config: {
            fields: [
                { name: 'sfbnr', type: 'int' },
                { name: 'sfbtext', type: 'string' },
                { name: 'leaf', type: 'boolean' }
            ],
            belongsTo: 'MyApp.model.BuildingElement'
        }
    });

  2. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,379

    Default

    When you have an association, the data is removed from the "main" record and a new record(s) is created from that data so you would have to use the get method to get the associated data.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  3. #3
    Sencha User
    Join Date
    Jan 2012
    Posts
    74

    Default

    Like this?
    Code:
    store.getAt(index).get('sfbresources')

  4. #4
    Sencha User ilija139's Avatar
    Join Date
    Aug 2011
    Location
    Macedonia
    Posts
    36

    Default

    You can even call:
    Code:
    store.getAt(index).sfbresources()
    http://docs.sencha.com/touch/2-0/#!/guide/models

  5. #5
    Sencha User
    Join Date
    Jan 2012
    Posts
    74

    Default

    Sitting here testing it as we speak.

    Neither get('sfbresources') nor sfbresources() yields a result.

    I can see that it has the correct model (MyApp.model.SfbResource) defined, but no data is put in the object.

  6. #6
    Sencha User ilija139's Avatar
    Join Date
    Aug 2011
    Location
    Macedonia
    Posts
    36

    Default

    Have you tried giving it a name like this:

    Code:
    hasMany: {model:'MyApp.model.SfbResource', name:'sfbresources'}

  7. #7
    Sencha User
    Join Date
    Jan 2012
    Posts
    74

    Default

    I have tried that just now, yeah. But with no different result.

  8. #8
    Sencha User rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,159

    Default

    Giving your hasMany assosiation an assosiationKey should fix your problem (it does for me when I test locally).

    Code:
    hasMany: {
        model: 'MyApp.model.SfbResource',
        associationKey: 'sfbresources',
        name: 'sfbresources'
    }
    More information: http://docs.sencha.com/touch/2-0/#!/...associationKey
    Sencha Inc.
    Robert Dougan - @rdougan
    Sencha Touch 2 and Ext JS 4 Core Team Member, SASS/Theming Wizard.

  9. #9
    Sencha User
    Join Date
    Jan 2012
    Posts
    74

    Default

    Superb, rdougan, that actually yielded a positive result. I now have an object called sfbresources with the right elements in them.

    A funny question though, when I try to put this data in the list. First of all, the primary records are put on the list, and the nested elements are put in as "null null" instead of with the right values. I guess that's because the listitems are based on two different models?

  10. #10
    Sencha User
    Join Date
    Jan 2012
    Posts
    74

    Default

    UPDATE: Nevermind this question, I found out that record.sfbresources() returns a store, so instead of using the setData()-method, I used setStore() and presto, I got the result I was looking for.

    Code:
    '<tpl for="sfbresources">'
         '<p class="buildingelement">',
              '<span class="number">{sfbnr}</span>&nbsp;',
              '<span class="description">{sfbtext}</span>',
         '</p>',
     '</tpl>'
    In a list this gives me a total of 5 items with all the childelements represented in each item, below them are 5 blank items. How can I get this to, first of all, be single items, and second of all, be one item per list on 5 cards, with single list items?

Page 1 of 2 12 LastLast

Posting Permissions

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