[英]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. 我正在尝试通过模型关联编写嵌套的XML数据,但我不能继续。
First of all the 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>
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. 当调用'success'函数时,我已经请求了所有XML,并且在控制台上写了“A card”。 Then, I try to change the name of the card in 'Another card' and send the data back with card.save () .
然后,我尝试在“另一张卡片”中更改卡片的名称,然后用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. 1)请求paylod(发送到服务器的数据)不是我在上一个请求中获得的XML。 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. 我得到了这个结构,因为作者:空两个相等的元素和一个新的元素('id')我没有指定任何地方。 So, the first question is: how can I add a single documentRoot or record element to the data I've to send?
所以,第一个问题是: 如何将单个documentRoot或记录元素添加到我要发送的数据中?
2) The second point is: where's my data? 2)第二点是: 我的数据在哪里? Why the sent XML is empty?
为什么发送的XML是空的? 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 . 3)最后,第三个问题:请求的Content-Type是text / xml 。 So, how can I change it into application/xml?
那么, 如何将其更改为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.
我想要的只是读取XML数据,修改一些字段然后再将其发回,所有内容都采用XML格式。
I asked on Sencha Forum and that's the answer : 我问Sencha论坛, 答案是 :
The writer does not support nesting at this time.
作者此时不支持嵌套。
Scott.
斯科特。
So, by for now, it's not possible ;) 那么,到现在为止,这是不可能的;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.