Javascript ExtJS4:何时使用完整名称空间与仅使用对象名称(模型关联)
我的项目模型的一部分:Javascript ExtJS4:何时使用完整名称空间与仅使用对象名称(模型关联),javascript,model,associations,extjs4,Javascript,Model,Associations,Extjs4,我的项目模型的一部分: Ext.define('DnD.model.Item', { extend: 'Ext.data.Model', idProperty:'item_number', associations: [{ type: 'belongsTo', model: 'Company', primaryKey: 'id', foreign
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'
}, {
...
公司模式
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();
}
});
问题
使用现在的代码,每当我创建项模型的新实例并尝试调用myItem.getCompany()
时,控制台就会抛出一个错误,告诉我我创建的对象没有这样的方法
现在,如果我将关联更改为表示某个项属于DnD.model.Company
(而不仅仅是Company
),则会创建一个getter方法,但它被称为getDnD.model.Company()
(而不仅仅是getCompany()
)
从我所看到的,似乎自动加载器无法找到模型,除非我提到完整的路径名。不过,请看一下我的采集控制器:
Ext.define('DnD.controller.Collections', {
extend: 'Ext.app.Controller',
models: [
'Collection'
],
stores: [
'Collections',
],
views:[
'collection.List'
],
refs: [{
ref: 'collectionList',
selector: 'collectionlist'
}],
init: function() {
}
});
请注意,我可以引用模型、存储和视图,而无需使用完整的名称空间
任何指导都将不胜感激。由于关联具有配置选项“getterName”和“setterName”,您可以使用它们定义自己的getter和setter方法名称。示例:{type:'belongsTo',model:'My.model.User',foreignKey:'userId',getterName:'getUser'}