我正在尝试通过模型关联编写嵌套的 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 ();
}
});
当“成功”函数被调用时,我得到了我请求的所有 XML,并且“A card”被写在了控制台上。然后,我尝试在“另一张卡”中更改卡的名称,并使用 card.save () 将数据发回。 在这一点上,我遇到了三种问题:
1) 请求有效负载(发送到服务器的数据)不是我在上一个请求中获得的 XML。事实上,它具有以下结构:
<?xml version="1.0" encoding="utf-8"?>
<card>
<card>
<id></id>
</card>
</card>
我得到这个结构是因为作者:空有两个相等的元素和一个新元素('id')我没有在任何地方指定。 因此,第一个问题是:如何将单个 documentRoot 或 record 元素添加到我要发送的数据中?
2) 第二点是:我的数据在哪里?为什么发送的 XML 是空的?我的模型保存过程是否正确?
3) 最后,第三个问题:请求的Content-Type 是text/xml。那么,如何将其更改为 application/xml?
模型协会在阅读方面没问题,但我无法真正理解他们如何在写作方面发挥作用。 我想要的只是读取 XML 数据,修改一些字段,然后再将其发回,一切都采用 XML 格式。
最佳答案
我在 Sencha 论坛和 that's the answer 上问过:
The writer does not support nesting at this time.
Scott.
所以,到目前为止,这是不可能的 ;)
关于javascript - ExtJS 4 : Write nested XML with model associations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12266753/