[英]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-Type是text / xml 。 那么, 如何将其更改为application / xml?
模型关联可以阅读,但我不能真正理解它们如何与写作一起工作。 我想要的只是读取XML数据,修改一些字段然后再将其发回,所有内容都采用XML格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.