简体   繁体   中英

ExtJS 4: Write nested XML with model associations

I'm trying to write nested XML data via model associations but I can't go ahead.

First of all the 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>

Here's the code of models:

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

As you can see, each model has his reader and writer (this last one is really needed?).

Here I retrieve data and I try to send it back to the server.

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

When 'success' function is called, I've all the XML I requested and 'A card' is written on the console. Then, I try to change the name of the card in 'Another card' and send the data back with card.save () . At this point, I got three kind of problems:

1) The request paylod (data sent to the server) isn't the XML I got on the previous request. In fact, it has the following structure:

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

I got this structure because of the writer: empty with two equal elements and a new element ('id') I didn't specify nowhere. So, the first question is: how can I add a single documentRoot or record element to the data I've to send?

2) The second point is: where's my data? Why the sent XML is empty? Am I doing right the model saving process?

3) And, in the end, the third problem: the Content-Type of the request is text/xml . So, how can I change it into application/xml?

Model associations are ok with reading but I can't really understand how they works with writing. What I want is just read the XML data, modify some fields and then send it back again, everything with the XML format.

I asked on Sencha Forum and that's the answer :

The writer does not support nesting at this time.

Scott.

So, by for now, it's not possible ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM