如何使用 ExtJS 5 Ext.data.Model 字段参考(与 ExtJS 4 中的 belongsTo 相比)

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

本文关键字:ExtJS 中的 相比 belongsTo 字段 何使用 Ext data Model 参考      更新时间:2023-09-26

我在 Ext.data.Model 中使用 belongsTo,它就像魅力一样工作,thread.getCustomer(function(record) {[…]})加载客户:

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

但是,我收到来自 Ext 的警告:

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

我试图将其转换为字段定义中的引用:

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

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

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

不起作用。

在 中找不到任何有用的东西http://docs.sencha.com/extjs/5.0/core_concepts/data_package.html或http://docs.sencha.com/extjs/5.0/whats_new/5.0/extjs_upgrade_guide.html

你们中有人运气好吗?

我遇到了完全相同的问题,这个链接帮助了我:http://www.sencha.com/forum/showthread.php?285478-Nested-stores-associated-model-doesn%C2%B4t-contain-any-store

它给了我这个:

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

现在我有一个一对多的关联,它有效:

 > 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"