簡體   English   中英

嵌套的XML數據和ExtJS模型關聯

[英]Nested XML data and ExtJS model associations

我正在嘗試將XML數據映射到ExtJS中的一系列模型。 我能夠從頂層模型中提取數據,但是與它的關聯無法檢索必要的數據。

以下是我的所有型號:

Ext.define('app.model.ProductType', {
    extend: 'Ext.data.Model',
    idProperty: 'id',
    fields: [{
        name: 'id',
        mapping: 'id',
        type: 'int'
    }, {
        name: 'name',
        mapping: 'name',
        type: 'string'
    }, {
        name: 'sometag',
        mapping: 'sometag',
        type: 'string'
    }],
    associations: [{
        type: 'hasMany',
        model: 'app.model.Address',
        name: 'addresses',
        autoLoad: true,
        reader: {
            type: 'xml',
            record: 'address',
            root: 'addressList'
        }
    }, {
        type: 'hasMany',
        model: 'app.model.PhoneContact',
        name: 'contacts',
        autoLoad: true,
        reader: {
            type: 'xml',
            record: 'phoneContact',
            root: 'phoneContactList'
        }
    }, {
        type: 'hasOne',
        model: 'app.model.OneToOne',
        name: 'oneToOne',
        autoLoad: true,
        reader: {
            type: 'xml',
            record: 'oneToOne'
        }
    }],
    proxy: {
        type: 'ajax',
        url: 'data/example.xml',
        reader: {
            type: 'xml',
            record: 'ProductType',
            root: 'ProductResultSet'
        }
    }
});

地址:

Ext.define('app.model.Address', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'city',
        mapping: 'city',
        type: 'string'
    }, {
        name: 'street',
        mapping: 'street',
        type: 'string'
    }],
    associations: [{
        type: 'belongsTo',
        model: 'app.model.ProductType'
    }]
});

PhoneContact:

Ext.define('app.model.PhoneContact', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'primary',
        mapping: 'primary',
        type: 'string'
    }, {
        name: 'cell',
        mapping: 'cell',
        type: 'string'
    }],
    associations: [{
        type: 'belongsTo',
        model: 'app.model.ProductType'
    }]
});

OneToOne:

Ext.define('app.model.OneToOne', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'first',
        mapping: 'firstfield',
        type: 'string'
    }, {
        name: 'second',
        mapping: 'secondfield',
        type: 'string'
    }],
    associations: [{
        type: 'belongsTo',
        model: 'app.model.ProductType'
    }]
});

這是我的app.js

Ext.Loader.setConfig({
    enabled: true
});

Ext.application({
    name: 'app',
    autoCreateViewport: false,
    requires: ['app.model.ProductType', 'app.model.Address', 'app.model.PhoneContact', 'app.model.OneToOne', 'app.store.ProductTypeStore'],
    launch: function () {
        app.model.ProductType.load(55, {
            scope: this,
            callback: function (record, operation) {
                console.log('Record Addresses:');
                console.log(record.addresses());
            }
        });

        var s = app.store.ProductTypeStore.create();
        s.on('load', this.wiggidy, this);
        s.load();
    },
    wiggidy: function (store, records, success, eOpts) {
        var rec = store.getAt(0);
        console.log('Associated Data:');
        console.log(rec.getAssociatedData());
    }
});

最后,example.xml

<ProductResultSet>
<ProductType>
    <id>55</id>
    <name>Product Name</name>
    <sometag>asdf</sometag>
    <addressList>
        <address>
            <street>12345 a</street>
            <city>myCity</city>
        </address>
        <address>
            <street>234567 b</street>
            <city>Denver</city>
        </address>
    </addressList>
    <phoneContactList>
        <phoneContact>
            <primary>234</primary>
            <cell>4667</cell>
        </phoneContact>
        <phoneContact>
            <primary>5467</primary>
            <cell>87904</cell>
        </phoneContact>
    </phoneContactList>
    <oneToOne>
        <firstField>value</firstField>
        <secondField>value</secondField>
    </oneToOne>
</ProductType>
</ProductResultSet>

控制台為RecordAddresses輸出吐出“Uncaught TypeError:無法調用方法'indexOf'的undefined”,然后我得到一個對象,其中填充了帶有0個對象的數據,用於關聯數據輸出。

我完全被這個困擾了。 我到處搜尋,這讓我感到很茫然。 為什么記錄沒有在該xml文件中加載嵌套數據?

任何幫助將不勝感激。

為什么在hasMany關系中設置autoLoad = true?

您正在嘗試使用嵌套數據,然后您告訴它按需加載數據,並想知道為什么沒有嵌套數據:)

您還缺少anchorKey,這是嵌套加載所必需的。

以下是您的代碼的工作示例:

http://jsfiddle.net/el_chief/668Fg/5/

將來遵循我的“規則”,一切都會好的:

http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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