Thank you for reporting this bug. We will make it our priority to review this report.
  1. #1
    Ext JS Premium Member
    Join Date
    May 2008
    Posts
    143
    Vote Rating
    0
    sg707 is on a distinguished road

      0  

    Default [FIXED]Model root is undefined error

    [FIXED]Model root is undefined error


    Code:
    Ext.regModel('User', {
        fields : [
                    {
                        name : 'id',
                        type : 'int'
                    }, {
                        name : 'name',
                        type : 'string'
                    }
        ],
        hasMany : 'Post',
        proxy : {
            type : 'rest',
            url : 'users',
            format : 'json',
            reader : {
                type : 'json',
                root : 'users'
            }
        }
    
    });
    
    Ext.regModel('Post', {
        fields : [
             {
                 name : 'title',
                 type : 'string'
             }
             ],
        belongsTo : 'User',
        
        proxy : {
            type : 'rest',
            url : 'posts',
            format : 'json',
            reader : {
                type : 'json' ,
                root : 'posts'
            }
        }
    
    });
    
    User.load('1', {
                          success : function(record, operations) {
                               console.log(record);
                       });
    As you can see, I'm creating Association Models w/ corresponding Proxy... however it's complaining that Post's root element is undefined. So if I comment out

    Code:
    root : 'posts'
    Then it works but this creates new problem...

    If I were to use Post.load method... it doesn't know what "root" is therefore the records does not get parse. Same thing for store

    Code:
    var postStore = new Ext.data.Store( {
        id : 'postStore',
        model : 'Post'
    });

  2. #2
    Ext JS Premium Member
    Join Date
    May 2008
    Posts
    143
    Vote Rating
    0
    sg707 is on a distinguished road

      0  

    Default


    Error
    Code:
    root is undefined
    total   = root.length,

  3. #3
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,989
    Vote Rating
    649
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    I'm not really clear on what you're saying, what does the data look like that's returned from your calls?
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  4. #4
    Ext JS Premium Member
    Join Date
    May 2008
    Posts
    143
    Vote Rating
    0
    sg707 is on a distinguished road

      0  

    Default


    Code:
    {
    • - users: [
      • - {
        • - posts: [
          • - {
            • title: "post 0"
            }
          • - {
            • title: "post 1"
            }
          • - {
            • title: "post 2"
            }
          ]
        • name: "John Lee"
        • id: 1
        }
      ]
    }
    Ignore the special character above as copy/paste from json viewer. Let me try to rephrase

    Code:
    Ext.regModel('User', {
        fields : [
                    {
                        name : 'id',
                        type : 'int'
                    }, {
                        name : 'name',
                        type : 'string'
                    }
        ],
        hasMany : 'Post',
        proxy : {
            type : 'rest',
            url : 'users',
            format : 'json',
            reader : {
                type : 'json',
                root : 'users'
            }
        }
    
    });
    Above code has many relationship w/ the Post Model

    Code:
    Ext.regModel('Post', {
        fields : [
            {
                name : 'title',
                type : 'string'
            }
        ],
        belongsTo : 'User'
        
    });
    With this the output from above is processed correctly. However, when I put proxy on "Post" model as below

    Code:
    Ext.regModel('Post', {
        fields : [
            {
                name : 'title',
                type : 'string'
            }
        ],
        belongsTo : 'User',
        proxy : {
            type : 'rest',
            url : 'posts',
            format : 'json',
            reader : {
                type : 'json',
                root : 'posts'
            }
        }
        
    });
    Then it fails saying that Post.proxy.reader.root is "undefined" when I clearly defined it as

    Code:
    root : 'posts'
    My guess is that User Model's "hasMany:Post" overrides the Post's root value. That's my wild guess.

  5. #5
    Ext JS Premium Member
    Join Date
    May 2008
    Posts
    143
    Vote Rating
    0
    sg707 is on a distinguished road

      0  

    Default


    Here is my entire code

    Code:
    
    Ext.regModel('User', {
        fields : [
                    {
                        name : 'id',
                        type : 'int'
                    }, {
                        name : 'name',
                        type : 'string'
                    }
        ],
        hasMany : 'Post',
        proxy : {
            type : 'rest',
            url : 'users',
            format : 'json',
            reader : {
                type : 'json',
                root : 'users'
            }
        }
    
    });
    
    Ext.regModel('Post', {
        fields : [
            {
                name : 'title',
                type : 'string'
            }
        ],
        belongsTo : 'User',
        proxy : {
            type : 'rest',
            url : 'posts',
            format : 'json',
            reader : {
                type : 'json',
                root : 'posts'
            }
        }
        
    });
    
    var userStore = new Ext.data.Store( {
        storeId  : 'dataStore',
        model : 'User'
    });
    
    var postStore = new Ext.data.Store( {
        storeId  : 'postStore',
        model : 'Post'
    });
    
    Ext.onReady(function() {
        var User = Ext.ModelMgr.getModel('User');
    
        
        User.load('1', {
            success : function (record, operations) {
               console.log('This Will Not Print!');
               console.log(arguments);
            }
        });
    
    });
    Run the above code w/ the same output from "users/1.json"

  6. #6
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    94
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    You could solve this with:
    Code:
    Ext.regModel('User', {
        fields: [{
            name: 'id',
            type: 'int'
        }, {
            name: 'name',
            type: 'string'
        }],
        hasMany: 'Post',
        proxy: {
            type: 'rest',
            url: 'users',
            format: 'json',
            reader: {
                type: 'json',
                root: 'users',
                getAssociatedDataRoot: function(data, associationName) {
                    if (associationName == 'posts') {
                        return data; // don't return data['posts'], because the reader 'root' is already set to 'posts'
                    }
                    return this.callOverridden(arguments);
                }
            }
        }
    });
    But it would prefer a better way to configure which reader should be used for associations (e.g. by using a getAssociatedReader method).

  7. #7
    Ext JS Premium Member
    Join Date
    May 2008
    Posts
    143
    Vote Rating
    0
    sg707 is on a distinguished road

      0  

    Default


    Bow to the genius!

    That worked brilliantly. So, I guess the way I did it is "technically correct" but I'm ok w/ this workaround.

  8. #8
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,989
    Vote Rating
    649
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Code:
    Ext.require('*');
    
    Ext.onReady(function(){
        Ext.regModel('User', {
            fields: [{
                name: 'id',
                type: 'int'
            }, {
                name: 'name',
                type: 'string'
            }],
            hasMany: {
                model: 'Post',
                reader: {
                    type: 'json'
                }
            },
            proxy: {
                type: 'ajax',
                url: 'users.json',
                reader: {
                    type: 'json',
                    root: 'users'
                }
            }
        });
        
        Ext.regModel('Post', {
            fields: [{
                name: 'title',
                type: 'string'
            }],
            belongsTo: 'User',
            proxy: {
                type: 'ajax',
                url: 'posts.json',
                reader: {
                    type: 'json',
                    root: 'posts'
                }
            }
        
        });
        
        var userStore = new Ext.data.Store({
            storeId: 'dataStore',
            model: 'User'
        });
        
        var postStore = new Ext.data.Store({
            storeId: 'postStore',
            model: 'Post'
        });
        
        var User = Ext.ModelMgr.getModel('User');
        
        
        User.load('1', {
            success: function(record, operations){
                console.log(record.posts().getCount());
            }
        });
        
        
    });
    Code:
    {
        "users": [
            {
                "posts": [
                    {
                        "title": "post  0" 
                    },
                    {
                        "title": "post 1" 
                    },
                    {
                        "title": "post 2" 
                    }
                ],
                "name": "John Lee",
                "id": 1 
            }
        ]
    }
    You can now specify a reader property on the associations, will be part of the next release.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

Similar Threads

  1. Showing error "root is undefined" where i am wrong?
    By jaisonjames in forum Ext 3.x: Help & Discussion
    Replies: 1
    Last Post: 6 Jan 2011, 12:57 AM
  2. [Solved]root is undefined - json error
    By michaelc in forum Ext 2.x: Help & Discussion
    Replies: 6
    Last Post: 24 Sep 2010, 2:09 AM
  3. [FIXED-187] The model data become undefined.
    By taka_2 in forum Sencha Touch 1.x: Bugs
    Replies: 5
    Last Post: 14 Aug 2010, 5:18 AM
  4. [Solved]Ext.data.JsonStore giving a "root is undefined" error.
    By nickweavers in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 26 Dec 2009, 11:05 AM
  5. Replies: 1
    Last Post: 29 Jul 2009, 7:09 PM

Thread Participants: 2