guess
guess

Reputation: 61

Extjs 5 data model - has many association

I'm trying to understand the new association concept in Extjs 5 data models.

I've got the following models

// User
Ext.define('App.model.User', {
    extend: 'App.model.Base',    
    fields: [
        {name: 'id', type: 'string'},
        {name: 'name', type: 'string'},
    ],    
    manyToMany: {
        Categories: {
            type: 'Categories',
            role: 'categories',
            field: 'categories',
            right: true
        }
    }
});

// Category
Ext.define('App.model.Category', {
    extend: 'App.model.Base',    
    constructor: function () {...},
    fields: [
        {name: 'id', type: 'string'},
        {name: 'categoryName', type: 'string'},
    ]
});

I've got the following json for a user:

 { "user": { "id": "1", "name": "Foo", "categories": [1, 2, 3] } }

When the User model is loaded it should initialize the categories store with the data.

(My Category model knows to convert the number to a object of id & categoryName)

For some reason when I try getting the users' categories the store is empty.

userRecord.categories(); // has no records

How can I get this to work?

Upvotes: 6

Views: 1255

Answers (1)

vmontanheiro
vmontanheiro

Reputation: 1039

Please try it

// User

Ext.define('User', {
    extend: 'app.data.Model',    
    fields: [
        {name: 'id', type: 'string'},
        {name: 'name', type: 'string'},
    ],    
    hasMany: {
        Categories: {
            type: 'Categories',
            role: 'categories',
            field: 'categories',
            right: true
        }
    }
});

Upvotes: 1

Related Questions