Loading hasMany data in ExtJS

13,585

Solution 1

Finally found the problem. For any future reference:

If you're using packages in the new MVC structure of ExtJS, define the full path to the linked class in your association like so:

hasMany: {model: 'Entrypage.model.EntrypageCriterium', name: 'brands', associationKey:'brands'}

Solution 2

You need to set the associationKey property in the hasMany association, so it knows which json property to use.

hasMany: {model: 'EntrypageCriterium',name:'brands', associationKey:'brands'}

see the Loading Nested Data section here:

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader

Share:
13,585
Bart Vangeneugden
Author by

Bart Vangeneugden

Hi, I'm a 28 year old webdevelopper. I code in Java, Javascript, PHP, SQL, Actionscript etc. I'm very interested in software design and algorithms

Updated on June 07, 2022

Comments

  • Bart Vangeneugden
    Bart Vangeneugden almost 2 years

    I'm trying to load 'nested' data in a hasMany relation in ExtJS4. My model looks like this:

    Ext.define("Entrypage.model.Entrypage",{
        extend: "Ext.data.Model",
        fields: ['id','title','urlkey','text','picture','keywords', 'searchterms','description','critriamodus'],
        hasMany: {model: 'EntrypageCriterium',name:'brands'},
        proxy: {
            type: 'ajax',
            url:  '/Admin/extjson/entrypages',
            reader: {type:'json', root:'entrypages'}
        }
    });
    

    And EntrypageCriterium:

    Ext.define("Entrypage.model.EntrypageCriterium",{
        extend: "Ext.data.Model",
        fields: ['id','type','title']
    });
    

    I load my data like so:

    Entrypage.load("nikon-coolpix",{success:function(record,options,success){
    console.log(record);
    }});
    

    It loads fine. Json returns this:

    {
        "success": true,        
    "entrypages":[{
        "id":"1",
        "urlkey":"nikon-coolpix",
        "title":"Nikon Coolpix",
        "text":"Some blahblah about Nikon",
        "keywords":"nikon,coolpix,digitale,camera",
        "description":"Nikon Coolpix camera's",
        "picture":"Nikon Coolpix camera's",
        "searchterms":"nikon coolpix",
        "language":"nl",
        "brands":[
            {"id":27038,"title":"Nikon","type":"brand"}
        ]   
    }]
    }
    

    But when I try record.brands() or anything like that. It says no such method exists. I think something is going wrong in mapping the data in the model.

    Any helpy would be very much appreciated!

  • Bart Vangeneugden
    Bart Vangeneugden over 12 years
    Sorry, didn't work. But wouldn't that make the brands method work, but return an empty list?
  • Neil McGuigan
    Neil McGuigan over 12 years
    I forgot the namespace on your model. Your foreignKey will also be wrong now, but you don't need to use it in this case.
  • o_nix
    o_nix almost 12 years
    Thank you. I have figured out the same situation myself but it was hard to do.
  • Tom O.
    Tom O. over 7 years
    Was the fix making the model fully qualified? or was the fix actually adding the associationKey?
  • JeSa
    JeSa almost 4 years
    Thnx, guys! For me, all this didn't work until I've added requires: ['Entrypage.model.EntrypageCriterium'] in Entrypage class