简体   繁体   English

ExtJS 6:通过创建模型的新实例来加载嵌套数据

[英]ExtJS 6 : load nested data by creating new instance of model

I try to create a model with an hasMany association but when I try to access to the store, it's empty. 我尝试使用hasMany关联创建模型,但是当我尝试访问商店时,它为空。

This is my models : 这是我的模特:

BaseModel : BaseModel:

Ext.define( 'Test.model.schema.BaseModel', {
    extend: 'Ext.data.Model',

    schema: {
        namespace: 'Test.model'
    }
} );

UserModel : UserModel:

Ext.define('Test.model.user.UserModel', {
    extend: 'Ext.data.Model',

    fields: [
        {
            name: 'displayName',
            type: 'string'
        }
    ],

    hasMany: [
        {
            name: 'roles',
            model: 'user.RoleModel', // also try with Test.model.user.RoleModel
            associationKey: 'roles'
        }
    ]
});

RoleModel : 榜样 :

Ext.define('Test.model.user.RoleModel', {
    extend: 'Ext.data.Model',

    fields: [
        {
            name: 'label',
            type: 'string'
        }
    ]
});

This is my Application : 这是我的应用程序:

Ext.application({
    name: 'Test',

    models : [
        'Test.model.schema.BaseModel',
        'Test.model.user.RoleModel',
        'Test.model.user.UserModel'
    ],

    appFolder : contextPath + '/' + staticsPath + '/js/app',

    controllers : ['router.TestRouterController'],

    defaultToken : 'auth'
});

In my controller I try to create my user model like this : 在我的控制器中,我尝试创建这样的用户模型:

var user = Ext.create('Test.model.user.UserModel', {
    displayName : 'Mick P.',
    roles : [
        {
            label: 'test'
        }
    ]
});

Same with JSon. 与JSon相同。

When I do user.roles().getAt(0) I got null and user.roles().data.items is empty. 当我执行user.roles()。getAt(0)时,我得到了null,user.roles()。data.items为空。

Do you see what i'm doing wrong ? 你看到我在做什么错吗?

EDIT 1 : A fiddle of my problem : https://fiddle.sencha.com/#fiddle/1e54 编辑1:我的问题的小提琴: https//fiddle.sencha.com/#fiddle/1e54

EDIT 2 : It works if I load my datas with a memory store. 编辑2:如果我用内存存储加载数据,它会起作用。 But why not by loading directly a model. 但是,为什么不直接加载模型呢?

Sencha support send to me a response. Sencha支持发送给我回复。 Perhaps some of you need an answer. 也许有些人需要答案。

First you need to read this documentation page : http://docs.sencha.com/extjs/6.0.2-classic/Ext.data.reader.Reader.html 首先,您需要阅读以下文档页面: http : //docs.sencha.com/extjs/6.0.2-classic/Ext.data.reader.Reader.html

When you pass data directly into the Model constructor, it is not passed through the configured reader, and only basic fields are evaluated. 当您将数据直接传递到Model构造函数时,它不会通过已配置的读取器传递,并且仅评估基本字段。

Data does need to pass through a Reader. 数据确实需要通过阅读器。

If you don't want to create a Store : 如果您不想创建商店:

1 - you need to declare proxy into models 1-您需要在模型中声明代理

2 - to create model with nested data, you have to do something like that : 2-要使用嵌套数据创建模型,您必须执行以下操作:

UserModel.getProxy().getReader().read(raw);

If you need a fiddle example tell me. 如果您需要一个小提琴的例子,请告诉我。

- Mickael -米凯尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM