簡體   English   中英

ExtJS4:何時使用完整命名空間 VS 僅使用 Object 名稱(模型關聯)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM