ExtJS 4:编写带有模型关联的嵌套XML

ExtJS 4: Write nested XML with model associations

本文关键字:关联 模型 嵌套 XML ExtJS      更新时间:2023-09-26

我正试图通过模型关联编写嵌套的XML数据,但我无法继续。

首先是XML:

<?xml version="1.0" encoding="utf-8"?>
<card>
    <generalData>
        <name>A card</name>
        <description>That's a description</description>
    </generalData>
    <specificData>
        <number>100</number>
        <type>int</type>
    </specificData>
    <otherStuff>
        <note>Those notes...</note>
    </otherStuff>
</card>

以下是型号代码:

Ext.define ('generalData', {
    extend: 'Ext.data.Model' ,
    fields: ['name', 'description']
});
Ext.define ('specificData', {
    extend: 'Ext.data.Model' ,
    fields: ['number', 'type']
});
Ext.define ('otherStuff', {
    extend: 'Ext.data.Model' ,
    fields: ['note']
});
Ext.define ('Card', {
    extend: 'Ext.data.Model' ,
    requires: ['generalData', 'specificData', 'otherStuff'] ,
    hasOne: [{
        name: 'generalData' ,
        model: 'generalData' ,
        getterName: 'getGeneralData' ,
        associationKey: 'generalData' ,
        reader: {
            type: 'xml' ,
            root: 'generalData' ,
            record: 'generalData'
        } ,
        writer: {
            type: 'xml' ,
            documentRoot: 'generalData' ,
            record: 'generalData'
        }
    } , {
        name: 'specificData' ,
        model: 'specificData' ,
        getterName: 'getSpecificData' ,
        associationKey: 'specificData' ,
        reader: {
            type: 'xml' ,
            root: 'specificData' ,
            record: 'specificData'
        } ,
        writer: {
            type: 'xml' ,
            documentRoot: 'specificData' ,
            record: 'specificData'
        }
    } , {
        name: 'otherStuff' ,
        model: 'otherStuff' ,
        getterName: 'getOtherStuff' ,
        associationKey: 'otherStuff' ,
        reader: {
            type: 'xml' ,
            root: 'otherStuff' ,
            record: 'otherStuff'
        } ,
        writer: {
            type: 'xml' ,
            documentRoot: 'otherStuff' ,
            record: 'otherStuff'
        }
    }] ,
    proxy: {
        type: 'ajax' ,
        url: '/card' ,
        reader: {
            type: 'xml' ,
            record: 'card' ,
            root: 'card'
        } ,
        writer: {
            type: 'xml' ,
            documentRoot: 'card' ,
            record: 'card' ,
            header: '<?xml version="1.0" encoding="utf-8"?>'
        }
    }
});

正如你所看到的,每个模型都有自己的读者和作者(最后一个真的需要吗?)。

在这里,我检索数据并尝试将其发送回服务器。

Card.load (1, {
    success: function (card) {
        console.log (card);
        var gd = card.getGeneralData (function (data) {
            console.log (data.get ('name'));
            data.set ('name', 'Another card');
        });
        card.save ();
    }
});

当调用"success"函数时,我得到了我请求的所有XML,并且在控制台上写下了"A card"。然后,我尝试在"另一张卡"中更改卡的名称,并使用card.save()将数据发回。在这一点上,我遇到了三种问题:

1) 请求paylod(发送到服务器的数据)不是我在上一个请求中得到的XML。事实上,它有以下结构:

<?xml version="1.0" encoding="utf-8"?>
<card>
    <card>
        <id></id>
    </card>
</card>

我得到这个结构是因为作者:空的,有两个相等的元素和一个新元素("id"),我没有指定任何地方。因此,第一个问题是:如何将单个documentRoot或记录元素添加到要发送的数据中

2) 第二点是:我的数据在哪里?为什么发送的XML为空?我在模型保存过程中做得对吗

3) 最后,第三个问题是:请求的内容类型text/xml。那么,如何将其更改为application/xml

模特协会可以阅读,但我真的不明白它们是如何与写作联系在一起的。我想要的只是读取XML数据,修改一些字段,然后再次将其发送回,所有内容都使用XML格式

我在Sencha论坛上问过,这就是答案

编写器目前不支持嵌套。

斯科特。

因此,到目前为止,这是不可能的;)