Bernd Goldschmidt
Bernd Goldschmidt

Reputation: 45

How to use ExtJS 5 Ext.data.Model Field reference (in comparison to belongsTo in ExtJS 4)

I use belongsTo in an Ext.data.Model and it works like charm, thread.getCustomer(function(record) {[…]}) loads a customer:

Ext.define('MyApp.model.Thread', {

    extend: 'MyApp.model.Base',

    requires: [
        'MyApp.model.Customer'
    ],

    idProperty: 'thread_id',

    fields: [
        {name: 'thread_id', type: 'int'},
        {name: 'thread_customer_id',type: 'int'},
    ],

    belongsTo: {
        model: 'MyApp.model.Customer',
        name: 'Customer',
        primaryKey: 'customer_id',
        foreignKey: 'thread_customer_id'
    }

});

However, I get a warning from Ext:

[W] Use of "belongsTo" is obsolete in MyApp.model.Thread

I tried to translate it to a reference in the field definition:

Ext.define('MyApp.model.Thread', {

    extend: 'MyApp.model.Base',

    requires: [
        'MyApp.model.Customer'
    ],

    idProperty: 'thread_id',

    fields: [
        {name: 'thread_id', type: 'int'},
        {
            name: 'thread_customer_id',
            type: 'int',
            reference: 'MyApp.model.Customer'
        }
    ]
});

or

reference: {
    type: 'MyApp.model.Customer',
    role: 'customer',
    association: 'Customer',
    inverse: 'thread'
}

or

reference: {
    type: 'Customer',
    role: 'customer',
    association: 'Customer',
    inverse: 'thread'
}

does not work.

Nothing helpful found in http://docs.sencha.com/extjs/5.0/core_concepts/data_package.html or http://docs.sencha.com/extjs/5.0/whats_new/5.0/extjs_upgrade_guide.html

Any of you had any luck with it?

Upvotes: 2

Views: 5166

Answers (1)

Laurent Mazuel
Laurent Mazuel

Reputation: 3546

I had exactly the same problem, this link helped me: http://www.sencha.com/forum/showthread.php?285478-Nested-stores-associated-model-doesn%C2%B4t-contain-any-store

It gave me this:

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

     schema: {
         namespace: 'MyApp.model'
     }
 });

 Ext.define('MyApp.model.Application', {
     extend: 'MyApp.model.Base',

     fields: [
        { name: 'id', type: 'int' },
        { name: 'name', type: 'auto' },
        { name: 'desc', type: 'auto' }
    ]
 });

 Ext.define('MyApp.model.ApplicationVersion', {
     extend: 'MyApp.model.Base',

     fields: [
         { name: 'id', type: 'int' },
         {
             name: 'appid',
             type: 'int',
             reference: {
                type: 'Application',
                role: 'application',
                inverse: 'versions'
             }
         },
         { name: 'version', type: 'auto' }
    ]
 });

And now I have a one-to-many association which works:

 > a = Ext.create(MyApp.model.Application, {desc: 'My description'})
   constructor {data: Object, session: null, internalId: 30, …}
 > a.versions().add({version: '2.5'})
   [constructor]
 > a.versions().first().application.get('desc')
   "My description"

Upvotes: 3

Related Questions