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