簡體   English   中英

ExtJS 4:使用模型關聯編寫嵌套XML

[英]ExtJS 4: Write nested XML with model associations

我正在嘗試通過模型關聯編寫嵌套的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)最后,第三個問題:請求的Content-Typetext / xml 那么, 如何將其更改為application / xml?

模型關聯可以閱讀,但我不能真正理解它們如何與寫作一起工作。 我想要的只是讀取XML數據,修改一些字段然后再將其發回,所有內容都采用XML格式。

我問Sencha論壇, 答案是

作者此時不支持嵌套。

斯科特。

那么,到現在為止,這是不可能的;)

暫無
暫無

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

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