Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extjs 5 data model - has many association

Tags:

extjs

extjs5

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?

like image 371
guess Avatar asked Oct 28 '14 11:10

guess


People also ask

How many types of proxies are there in ExtJS?

There are two main types of Proxy - Ext.

What is requires ExtJS?

You can look at requires as a way to tell ExtJS: "When you construct an object of this class, please make sure to dynamically load the required scripts first".

What is namespace in ExtJS?

The Ext namespace (global object) encapsulates all classes, singletons, and utility methods provided by Sencha's libraries.

What is controller in ExtJS?

The controller (Ext. app. Controller) in ExtJS 4. x is used to handle events from multiple components using CSS-like selectors to match components and respond to their events.


1 Answers

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
        }
    }
});
like image 194
vmontanheiro Avatar answered Sep 27 '22 21:09

vmontanheiro