6
votes

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?

1
Do you really want a m-m? Where is it supposed to get the categories from?Evan Trimboli
@EvanTrimboli: I really need it to be mapped one way and I've got a categories map hard coded, so I can convert the Id to it's name. what am I missing here?guess

1 Answers

1
votes

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
        }
    }
});