javascript - 如何使用 ExtJS 5 Ext.data.Model 字段引用(与 ExtJS 4 中的 ownTo 相比)

标签 javascript extjs extjs4 relationship extjs5

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

关于javascript - 如何使用 ExtJS 5 Ext.data.Model 字段引用(与 ExtJS 4 中的 ownTo 相比),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109594/

相关文章:

javascript - 检查 localStorage 中是否存在变量的值

web-applications - 使用 Play 框架和 sencha 的 Web 应用程序

javascript - 搜索 Sencha TreePicker

javascript - 您是否将 jQuery、extJS 或其他 JavaScript 库与 Actionscript 和 Flex 一起使用?

ExtJS:获取 ExtJS 样式的当前 URL

javascript - 如何在 sweet alert 2 中启用输入确认按钮

javascript - 使用纯 Javascript 创建输入字段

javascript - Chrome 扩展代码 vs 内容脚本 vs 注入(inject)脚本

ExtJS 4.2.1 - 无法从 Controller 获取 View 实例

javascript - ExtJS4 变换组合框 onchange() 事件不起作用