0
votes

I try to setup a hasMany relationship and want load my main entity with all the associated models in a single request. But that seems not to work with "hasMany" relations that is inherited. I have a BaseModel that defines all relations and fields and a "normal" model that defines the proxy to load from.

These are my (relevant) models:

Ext.define('MyApp.model.BaseUser', {
    "extend": "Ext.data.Model",
    "uses": [
        "MyApp.model.UserEmail",
        "MyApp.model.Account"
    ],
    "fields": [
        {
            "name": "name"
        },
        {
            "name": "accountId",
            "reference": {
                "type": "MyApp.model.Account",
                "role": "account",
                "getterName": "getAccount",
                "setterName": "setAccount",
                "unique": true
            }
        }
    ]
    "hasMany": [
        {
            "name": "emails",
            "model": "MyApp.model.UserEmail"
        }
    ],
});

Ext.define('MyApp.model.User', {
    extend: "MyApp.model.BaseUser",
    proxy: {
        type: 'rest',
        url : '/api/user',
        reader: {
            type: 'json',
            rootProperty: 'data',
        }
    }
});

Ext.define('MyApp.model.UserEmail', {
    extend: 'Ext.data.Model',

    "fields": [
        {
            "name": "id",
            "type": "int"
        },
        {
            "name": "email",
            "type": "string"
        },
    ],

    proxy: {
        type: 'rest',
        url : '/api/user/email',
        reader: {
            type: 'json',
            rootProperty: 'data',
        }
    }
});

// MyApp.model.Account looks like MyApp.model.UserEmail

This is my server's response:

{
    data: [
        {
            name: 'User Foo'
            accountId: 50
            account: {
                id: 50,
                balance: 0
            },
            emails: [
                {
                    id: 70,
                    email: '[email protected]'
                }
            ]
        }    
    ]
}

The "account" relation is working on the "normal" User Model and I can access it via the auto-generated method user.getAccount() as I expected.

Now I tried to access the users emails with the auto-generated methods:

// user is of 'MyApp.model.User'
user.emails(); // store
user.emails().first(); // null
user.emails().count(); // 0

It seems that the "emails"-relation models were not loaded into my user model. Am I accessing them the right way? I can access them via user.data.emails. But this is an array of plain objects, not of UserEmail-Objects.

Can anyone give me some advice? Is nested loading supported with keyless associations?

Kind regards, czed

Edit: Clearified what I ment.

1

1 Answers

0
votes

It should work. Here's a working fiddle. Check console for nested loaded data.