javascript 在 ExtJS 中加载 hasMany 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8134420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:25:26  来源:igfitidea点击:

Loading hasMany data in ExtJS

javascriptjsonextjsextjs4

提问by Bart Vangeneugden

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

我正在尝试hasMany在 ExtJS4中的关系中加载“嵌套”数据。我的模型看起来像这样:

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:

并且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:

它加载良好。Json 返回这个:

{
    "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.

但是当我尝试record.brands()或类似的事情时。它说不存在这样的方法。我认为在映射模型中的数据时出了点问题。

Any helpy would be very much appreciated!

任何帮助将不胜感激!

回答by Bart Vangeneugden

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:

如果您在 ExtJS 的新 MVC 结构中使用包,请在关联中定义链接类的完整路径,如下所示:

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

回答by Neil McGuigan

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

你需要在hasMany关联中设置associationKey属性,这样它就知道使用哪个json属性了。

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

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