1

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.

2
  • Why did you create a base model and schema but you aren't using it? If you are going to use the base model, schema and associations then your other models need to extend the base model. Commented Jul 25, 2016 at 22:58
  • Exact I don't need to Base but no need to extend Base. I modified my fiddle. Now User doesn't need to reference absolute path for Fiddle.model.Role. Just need to tell Role because I defined namspaces in Base model. Try to delete Base model and you will see the difference. Commented Jul 27, 2016 at 8:24

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.