1. #1
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default Unanswered: hasMany associattion error

    Unanswered: hasMany associattion error


    All,
    I'm having issues with a jsonstore. I'm following this example:http://jsfiddle.net/alexrom7/4Zs5H/1/

    I'm getting an error when I reference the hasMany model 'Submission":
    --[08:41:50.575] TypeError: data is undefined @ http://localhost:57376/ext-4.2.0.663...-debug.js:5712

    If I remove the SubMissions_Model, I do get the one record I expect. Normally, I'm loading this from a generic handler. that works correctly but I have created a variable mydata for this example. (the handler does not produce a 'data:..." entry.

    What am I doing wrong?

    here's my code
    Code:
            Ext.Loader.setConfig({ enabled: true });
            Ext.Loader.setPath('Ext.ux.DataView', 'ux/DataView');
            Ext.require([
                'Ext.data.*',
                'Ext.util.*',
                'Ext.view.View',
                'Ext.ux.DataView.Animated',
                'Ext.XTemplate',
                'Ext.panel.Panel',
                'Ext.toolbar.*',
                'Ext.slider.Multi'
            ]);
    
            Ext.onReady(function () {
                //data to be loaded into the ArrayStore
    
                var mydata = {
                    data: {
                        "Mission_ID": 1, "Mission_Name": "Mission 1", "Submissions": [
                            {
                                "Submission_ID": 1, "Submission_Name": "Submission 1"
                            }, {
                                "Submission_ID": 2, "Submission_Name": "Submission 2"
                            }]
                    }
                }
    
                Ext.define('Mission_Model', {
                    extend: 'Ext.data.Model',
                    idProperty: 'Mission_ID',
                    //root: 'd',
                    fields: [
                        { name: 'Mission_ID', mapping: 'Mission_ID', type: 'int' },
                        { name: 'Mission_Name', mapping: 'Mission_Name', type: 'string' }],
                    hasMany: { model: 'Submission_Model', name: 'Submissions' }       
                });
    
                Ext.define('Submission_Model'), {
                    extend: 'Ext.data.Model',
                    belongsTo: 'Mission_Model',
                    fields: [
                        { name: 'Submission_ID', mapping: 'Submission_ID', type: 'int' },
                        { name: 'Submission_Name', mapping: 'Submission_Name', type: 'string' }]
                };
    
                Ext.create('Ext.data.Store', {
                    storeId: 'Missions_Store',
                    model: 'Mission_Model',
                    autoLoad: true,
                    data: mydata,
                    proxy: {
                        type: 'memory',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    }
                });
    
                //var Missions_Store = new Ext.data.JsonStore({
                //    autoLoad: true,
                //    method: 'POST',
                //    model: 'Mission_Model',
                //    id: 'Mission_ID',
                //    proxy: new Ext.data.HttpProxy({
                //        url: 'data/getmissions.ashx'//
                //    }),
                //    listeners: {
                //        load: function (t, records, options) {
    
                //            Missions_Store.data.each(function (rec) {
                //                console.dir(rec.data)
                //            })}}});
    
                var dataview = Ext.create('Ext.view.View', {
                    deferInitialRefresh: false,
                    store: Missions_Store,
                    tpl: Ext.create('Ext.XTemplate',
                        '<tpl for=".">',
                            '<div><div class="base inner shadow shadow5">',
                                '<strong>{Mission_Name}</strong>',
                            '</div>',
                            '<tpl for="(Submissions)">',
                                '<div class="base inner shadow shadow3">',
                                '<strong>{Submission_ID}</strong>',
                                '</div>',
                            '</tpl></p>',
                            '</div>',
                        '</tpl>'
                    ),
    
                    plugins: [
                        Ext.create('Ext.ux.DataView.Animated', {
                            duration: 1000,
                            idProperty: 'Mission_ID'
                        })
                    ],
                    id: 'phones',
    
                    itemSelector: 'div.base',
                    overItemCls: 'phone-hover',
                    multiSelect: true,
                    autoScroll: true
                });

  2. #2
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default anyone? hello?

    anyone? hello?


    is this thing on?

  3. #3
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    5,021
    Answers
    385
    Vote Rating
    183
    slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold

      0  

    Default


    I'm not used to seeing the data consumed by associated models flat - it's always nested. Do you have control over the data from the source?

  4. #4
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default


    I can structure the data however is most convenient. I think the data provided above is nested. "Submissions" indicate child records. The final interface should be similar to the image below. The example above should produce the first two levels but I'd like to think you could carry to example out to an infinite number of levels and nodes. Any advice on how to build the models, json is appreciated.



    Thanks,

    Jody

  5. #5
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default


    Any help would be appreciated, please.

  6. #6
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    5,021
    Answers
    385
    Vote Rating
    183
    slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold slemmon is a splendid one to behold

      0  

    Default


    If you are able to deliver down the data all at once you might nest it together. Below is an example setup of three connected models and a nested data example to show how the data would look as its loaded by the reader to the model (and its associated model stores). Each associated models' data is found as a valueset under a property of the owner model's dataset.

    Example models
    Code:
    Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: ['id', 'name'],
        proxy: {
            type: 'rest',
            url: 'data/users',
            reader: {
                type: 'json',
                root: 'users'
            }
        },
    
    
        hasMany: 'Post' // shorthand for { model: 'Post', name: 'posts' }
    });
    
    
    Ext.define('Post', {
        extend: 'Ext.data.Model',
        fields: ['id', 'user_id', 'title', 'body'],
    
    
        proxy: {
            type: 'rest',
            url: 'data/posts',
            reader: {
                type: 'json',
                root: 'posts'
            }
        },
        belongsTo: 'User',
        hasMany: {
            model: 'Comment',
            name: 'comments'
        }
    });
    
    
    Ext.define('Comment', {
        extend: 'Ext.data.Model',
        fields: ['id', 'post_id', 'name', 'message'],
    
    
        belongsTo: 'Post'
    });
    Example data:
    Code:
    {
        success: true,
        users: [{
                id: 1,
                name: 'Ed',
                age: 25,
                gender: 'male',
                posts: [{
                        id: 12,
                        title: 'All about data in Ext JS 4',
                        body: 'One areas that has seen the most improvement...',
                        comments: [{
                                id: 123,
                                name: 'S Jobs',
                                message: 'One more thing'
                            }
                        ]
                    }
                ]
            }
        ]
    }
    If you haven't seen the Data Package guide and its subsection on Associations you might also check it out:
    http://docs.sencha.com/extjs/4.2.0/#!/guide/data

  7. #7
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default


    Thank you. I will apply this solution and report status.

  8. #8
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default very close...

    very close...


    I'm almost there. What have I done here that keeps me from seeing gchildren? I cant get to that third level. thanks again.

    here's the fiddle:
    http://jsfiddle.net/vdmzh/5/

    and the code.

    Code:
    Ext.define('ParentModel', {
        extend: 'Ext.data.Model',
        fields: [{
            name: 'parentField',
            type: 'string'
        }],
    
        hasMany: {
            model: 'ChildModel',
            name: 'children'
        }
    });
    
    Ext.define('ChildModel', {
        extend: 'Ext.data.Model',
        fields: [{
            name: 'childField',
            type: 'string'
        }],
        hasMany: {
            model: 'gChildModel',
            name: 'gchildren'
        }
    });
    
    Ext.define('gChildModel', {
        extend: 'Ext.data.Model',
        fields: [{
            name: 'gchildField',
            type: 'string'
        }]
    })
    
    Ext.require('*');
    
    Ext.onReady(function () {
    
        var store = Ext.create('Ext.data.Store', {
            model: 'ParentModel'
        });
    
        store.loadRawData([{
            parentField: "parent1",
            children: [{
                childField: "child1"
            }, {
                childField: "child2"
            }]
        }, {
            parentField: "parent2",
            children: [{
                childField: "child3"
            }, {
                childField: "child4",
                gchildren: [{
                    gchildField: "gchild1"
                }]
            }]
        }]);
    
        Ext.create('Ext.DataView', {
            renderTo: Ext.getBody(),
            store: store,
            itemTpl: ['<table border="1"><tr><td>{parentField}</td></tr><tr><td><table border=1><tr><tpl for="children"><td>{childField}</td></tpl></tr><tr><td><table border=1><tr><tr><tpl for="gchildren"><td>{gchildField}</td></tpl></tr></table></td></tr></table></td></tr></table>']
        });
    
    })

  9. #9
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default almost there....

    almost there....


    It's merely a matter of css tricks now...

    http://jsfiddle.net/vdmzh/10/

  10. #10
    Sencha User
    Join Date
    Mar 2011
    Posts
    12
    Vote Rating
    0
    jodyford is on a distinguished road

      0  

    Default I got it. Please mark as answered

    I got it. Please mark as answered


    Thanks!

Thread Participants: 1

Tags for this Thread