简体   繁体   English

ExtJS4:何时使用完整命名空间 VS 仅使用 Object 名称(模型关联)

[英]ExtJS4: When to Use Full Namespace VS Just Object Name (Model Associations)

Part of My Item Model:我的项目 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'
        }, {
...

The Company Model本公司 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'
        }
    }
})

The app.js file 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();        
    }
});

The Problem问题

With the code the way it is now, whenever I make a new instance of the item model and attempt to call myItem.getCompany() the console throws an error telling me that the object I created has no such method.使用现在的代码,每当我创建项目 model 的新实例并尝试调用myItem.getCompany()时,控制台都会抛出一个错误,告诉我我创建的 object 没有这种方法。

Now, if I change the association to say that an Item belongs to DnD.model.Company (as opposed to just Company ), a getter method is created, but it's called getDnD.model.Company() (as opposed to just getCompany() ).现在,如果我将关联更改为说一个 Item 属于DnD.model.Company (而不是只是Company ),则会创建一个 getter 方法,但它被称为getDnD.model.Company() (而不是只是getCompany() )。

From what I can see, it appears that the autoloader can't find the model unless I mention the full path name.据我所见,除非我提及完整路径名,否则自动加载器似乎找不到 model。 However, take a look at my Collection Controller:但是,看看我的 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() {
    }
});

Notice I can reference the models, stores and views without using the full namespace.请注意,我可以在不使用完整命名空间的情况下引用模型、商店和视图。

Any guidance would be very appreciated.任何指导将不胜感激。

BelongsTo association has config options "getterName" and "setterName", you can use them to define your own getter and setter method names. 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 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM