嵌套的XML数据和ExtJS模型关联

3
我将尝试将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'
    }]
});

电话联系方式:
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'
    }]
});

一对一:
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输出抛出“未捕获的TypeError:无法调用未定义的方法'indexOf'”,然后我得到一个填充了0个对象的数组的对象,用于关联数据输出。
我完全被卡住了。我已经在各处搜索了,但只能走到这一步。为什么记录没有加载xml文件中的嵌套数据?
非常感谢任何帮助。

你能否发布你的工作样例? - user203687
1个回答

4

谢谢您的回复。我喜欢“规则”部分 - 看起来我几乎违反了所有规则:D。目前我无法给您点赞,因为我需要达到15的声望。我会尝试您给我的链接,如果成功了,我会将其标记为已接受的答案。 - guitarist809
哇,我简直不敢相信解决问题的是associationKey。非常感谢!一旦我能够,我一定会给你的答案点赞。 - guitarist809

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接