我会尝试通过模型关联来编写嵌套的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”。然后,我尝试将卡片名称更改为“Another card”,并使用card.save()将数据发送回服务器。 此时,我遇到了三种问题:
1)请求有效负载(发送到服务器的数据)不是我之前请求得到的XML。事实上,它具有以下结构:
<?xml version="1.0" encoding="utf-8"?>
<card>
<card>
<id></id>
</card>
</card>
因为写手的原因,我得到了这个结构:空的、有两个相等元素和一个新元素('id'),我没有在任何地方指定它。所以,第一个问题是:如何向我要发送的数据中添加单个documentRoot或record元素? 第二个问题是:我的数据在哪里?为什么发送的XML是空的?我是否正确执行了模型保存过程? 最后,第三个问题是:请求的Content-Type是text/xml。那么,如何将其更改为application/xml?模型关联在读取方面很好,但我真的不明白它们在写入方面是如何工作的。我想要的只是读取XML数据,修改一些字段,然后再次以XML格式发送回去。