Results 1 to 4 of 4

Thread: Nested Model not loading hasmany

  1. #1
    Sencha User
    Join Date
    Aug 2016
    Posts
    14
    Answers
    1

    Default Answered: Nested Model not loading hasmany

    Hi guys!!

    I'm having a little problem with nested models and, although I searched and followed a bunch of tutorials, I couldn't get it right.

    I have the following JSON that has 3 registers and the last one has one child.

    Code:
    {
        "data" : [{
                "iconCls" : "x-fa fa-desktop",
                "id" : 1,
                "leaf" : true,
                "parentId" : 0,
                "text" : "Trabalhos"
            }, {
                "iconCls" : "x-fa  fa-paper-plane",
                "id" : 2,
                "leaf" : true,
                "parentId" : 0,
                "text" : "E-mail"
            }, {
                "children" : [{
                        "iconCls" : "x-fa user",
                        "id" : 4,
                        "leaf" : true,
                        "parentId" : 3,
                        "text" : "Editar"
                    }
                ],
                "iconCls" : "x-fa fa-users",
                "id" : 3,
                "leaf" : false,
                "parentId" : 0,
                "text" : "Usuários"
            }
        ]
    }

    I made the following Models: Menu and MenuChild

    Menu
    Code:
    Ext.define('PlataformaFluxo.model.menu.Menu', {
        extend: 'Ext.data.Model',
    
        requires: [
            'PlataformaFluxo.model.menu.MenuChild'
        ],
    
        fields: ['id', 'text', 'iconCls','leaf', 'parentId'],
    
        hasMany: {
            model: 'PlataformaFluxo.model.menu.MenuChild',    
            name: 'children',
            associationKey: 'children'
        },
        
        proxy:{
            type: 'ajax',
            url:'treelistMenu.action',
            noCache: false,
            reader:{
                type: 'json',
                rootProperty: 'data'
            }
        }
        
    });

    MenuChild:
    Code:
    Ext.define('PlataformaFluxo.model.menu.MenuChild', {
        extend: 'Ext.data.Model',
    
        fields: ['id', 'text', 'iconCls', 'leaf', 'parentId']
        
    });

    Sencha loads fine the 3 registers, but it doesn't get the children from the last one (childNodes remains empty).

    What I am doing wrong?


    Thanks!!

  2. I found out!

    The associationKey and name had the same value and that was, somehow, messing with everything!

  3. #2
    Sencha User
    Join Date
    Dec 2015
    Posts
    118
    Answers
    12

    Default

    Are you loading the data manuall?
    If it is the case, you have to load through the store's proxy.
    On your tree store you would define a memory proxy and set the reader as json.
    To load the data, you would need to get the proxy on the store then call setData().
    Then call load() on the tree store.
    Hope it helps

  4. #3
    Sencha User
    Join Date
    Aug 2016
    Posts
    14
    Answers
    1

    Default

    Thanks for your answer!

    I'm trying to remake the Loading Nested Data from http://docs.sencha.com/extjs/6.0.2/m...er.Reader.html but it still doesn't work.


    Here is my code ( I changed the name from children to menuChildren)
    Code:
           
            var store = Ext.create('Ext.data.Store', {
                                        model: 'PlataformaFluxo.model.menu.Menu'
                                    });
            store.load({
                callback: function() {              
                    store.data.items.forEach(function(item, index){
                        if (item.get('menuChildren') != undefined) {
                            alert('children here');
                        }
                    });
                    
                }            
            });

    Models
    Code:
    Ext.define('PlataformaFluxo.model.menu.Menu', {
        extend: 'Ext.data.Model',
    
        requires: [
            'PlataformaFluxo.model.menu.MenuChild'
        ],
    
        fields: ['id', 'text', 'iconCls','leaf', 'parentId'],
    
        hasMany: {
            model: 'PlataformaFluxo.model.menu.MenuChild',    
            name: 'menuChildren',
            associationKey: 'menuChildren'
        },
        
        proxy:{
            type: 'ajax',
            url:'treelistMenu.action',
            noCache: false,
            reader:{
                type: 'json',
                rootProperty: 'data'
            }
        }
    });
    
    
    Ext.define('PlataformaFluxo.model.menu.MenuChild', {
        extend: 'Ext.data.Model',
    
        fields: ['id', 'text', 'iconCls', 'leaf', 'parentId']
        
    });

    Inside the callback function, I want to see the response from server, including the all the menuChildren.
    For all other parameters I can get with store.data.items[index].get('parameter here'), but when it comes to menuChildren it returns me undefined.
    Debugging my code I noticed that the object has a constructor _menuChildren (picture below). Am I trying to get the children too soon?
    debug.jpg

  5. #4
    Sencha User
    Join Date
    Aug 2016
    Posts
    14
    Answers
    1

    Default

    I found out!

    The associationKey and name had the same value and that was, somehow, messing with everything!

Similar Threads

  1. Loading nested XML data into hasmany model
    By mjankela in forum Ext: Q&A
    Replies: 3
    Last Post: 22 Mar 2015, 6:22 PM
  2. Loading nested XML data into hasmany model
    By mjankela in forum Community Discussion
    Replies: 0
    Last Post: 14 Mar 2015, 7:16 AM
  3. AJAX Store & loading data from nested hasMany association
    By kitesurf in forum Sencha Touch 2.x: Q&A
    Replies: 3
    Last Post: 10 Feb 2014, 11:32 AM
  4. Children elements aren't show using nested json, Model and hasMany association.
    By dusoft in forum Sencha Architect 2.x: Help & Discussions
    Replies: 2
    Last Post: 18 Jul 2013, 11:33 AM
  5. Model with nested hasmany, empties child record on model.load(
    By salorob in forum Sencha Touch 2.x: Q&A
    Replies: 3
    Last Post: 18 Aug 2012, 1:24 AM

Tags for this Thread

Posting Permissions

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