Mickaël P
Mickaël P

Reputation: 365

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.

This is my models :

BaseModel :

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

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

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.

When I do user.roles().getAt(0) I got null and user.roles().data.items is empty.

Do you see what i'm doing wrong ?

EDIT 1 : A fiddle of my problem : https://fiddle.sencha.com/#fiddle/1e54

EDIT 2 : It works if I load my datas with a memory store. But why not by loading directly a model.

Upvotes: 1

Views: 1769

Answers (1)

Mickaël P
Mickaël P

Reputation: 365

Sencha support send to me a response. 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

When you pass data directly into the Model constructor, it is not passed through the configured reader, and only basic fields are evaluated.

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

2 - to create model with nested data, you have to do something like that :

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

If you need a fiddle example tell me.

- Mickael

Upvotes: 2

Related Questions