[英]ExtJS4: When to Use Full Namespace VS Just Object Name (Model Associations)
我的项目 Model 的一部分:
Ext.define('DnD.model.Item', {
extend: 'Ext.data.Model',
idProperty:'item_number',
associations: [{
type: 'belongsTo',
model: 'Company',
primaryKey: 'id',
foreignKey: 'company_id',
autoLoad: true
}],
proxy: {
type: 'ajax',
url: 'data/items.json',
reader: {
type: 'json',
root: 'items',
idProperty:'id'
}
},
fields: [{
name: 'id',
type: 'int'
},{
name: 'box_number',
type: 'float'
}, {
name: 'item_number',
type: 'float'
}, {
name: 'name',
type: 'string'
},{
name: 'format',
type: 'int'
}, {
name: 'company_id',
type: 'int'
}, {
...
本公司 Model
Ext.define('DnD.model.Company', {
extend: 'Ext.data.Model',
associations: [{
type: 'hasMany',
model: 'Item',
primaryKey: 'id',
foreignKey: 'company_id',
autoLoad: true
}],
idProperty:'id',
fields: [{
name: 'id',
type: 'int'
}, {
name: 'name',
type: 'string'
}],
proxy: {
type: 'ajax',
url: 'data/companies.json',
reader: {
successProperty: true,
type: 'json',
root: 'companies',
idProperty:'id'
}
}
})
app.js 文件
Ext.Loader.setConfig({
enabled: true,
paths: {
Ext: 'extjs/src',
My: 'app'
}
});
Ext.application({
name: 'DnD',
appFolder: 'app',
autoCreateViewport: false,
controllers: [
'Collections',
'Items'
],
launch: function() {
this.viewport = Ext.create('DnD.view.Viewport', {});
this.viewport.show();
}
});
问题
使用现在的代码,每当我创建项目 model 的新实例并尝试调用myItem.getCompany()
时,控制台都会抛出一个错误,告诉我我创建的 object 没有这种方法。
现在,如果我将关联更改为说一个 Item 属于DnD.model.Company
(而不是只是Company
),则会创建一个 getter 方法,但它被称为getDnD.model.Company()
(而不是只是getCompany()
)。
据我所见,除非我提及完整路径名,否则自动加载器似乎找不到 model。 但是,看看我的 Collection Controller:
Ext.define('DnD.controller.Collections', {
extend: 'Ext.app.Controller',
models: [
'Collection'
],
stores: [
'Collections',
],
views:[
'collection.List'
],
refs: [{
ref: 'collectionList',
selector: 'collectionlist'
}],
init: function() {
}
});
请注意,我可以在不使用完整命名空间的情况下引用模型、商店和视图。
任何指导将不胜感激。
BelongsTo 关联具有配置选项“getterName”和“setterName”,您可以使用它们来定义自己的 getter 和 setter 方法名称。 Example: {type: 'belongsTo', model: 'My.model.User', foreignKey: 'userId', getterName: 'getUser'} http://docs.sencha.com/ext-js/4-0/# /api/Ext.data.BelongsToAssociation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.