简体   繁体   English

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

[英]Nested XML data and ExtJS model associations

I'm trying to map XML data to a series of models in ExtJS. 我正在尝试将XML数据映射到ExtJS中的一系列模型。 I'm able to extract the data from the toplevel model, however, the associations with it fail to retrieve the necessary data. 我能够从顶层模型中提取数据,但是与它的关联无法检索必要的数据。

Here are all of my models: 以下是我的所有型号:

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'
        }
    }
});

Address: 地址:

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: 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: 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'
    }]
});

Here's my app.js 这是我的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());
    }
});

And finally, example.xml 最后,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>

The console spits out "Uncaught TypeError: Cannot call method 'indexOf' of undefined" for the RecordAddresses output, and then I get an object filled with arrays with 0 objects for the Associated Data output. 控制台为RecordAddresses输出吐出“Uncaught TypeError:无法调用方法'indexOf'的undefined”,然后我得到一个对象,其中填充了带有0个对象的数据,用于关联数据输出。

I'm completely stumped on this one. 我完全被这个困扰了。 I've searched around everywhere and it's taken me this far. 我到处搜寻,这让我感到很茫然。 Why is the record not loading the nested data in that xml file? 为什么记录没有在该xml文件中加载嵌套数据?

Any help will be greatly appreciated. 任何帮助将不胜感激。

Why are you setting autoLoad=true in the hasMany relationship? 为什么在hasMany关系中设置autoLoad = true?

You are trying to use nested data, and then you are telling it to load the data on demand, and wondering why there is no nested data :) 您正在尝试使用嵌套数据,然后您告诉它按需加载数据,并想知道为什么没有嵌套数据:)

You are also missing the associationKey, which is required for nested loading. 您还缺少anchorKey,这是嵌套加载所必需的。

Here is a working example of your code: 以下是您的代码的工作示例:

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

Follow my "Rules" in the future and everything will be ok: 将来遵循我的“规则”,一切都会好的:

http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html 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