我有两种模式:Book belongsTo Author。让我们来看看作者的数据在从服务器到ExtJs的路上返回时与书籍数据一起发送。服务器发送下面的嵌套数据ExtJS的:在ExtJS中加载并保存嵌套数据4
{
success : true,
data: {
id: '23',
name: 'JavaScript - The definitive guide'
author: { // <<-- nested author data
id: '45',
name: 'David Flanagan',
}
}
}
在ExtJS的侧面的数据通过下面的模型处理:
Ext.define('My.models.Book', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [
'id',,
'name',
{name: 'author_id', mapping: 'Author.id'}, // <<-- flat author data
],
proxy: {
type: 'ajax',
api: {
read: 'My/Books/index',
create: 'My/Books/create',
update: 'My/Books/update',
destroy: 'My/Books/delete'
},
reader: {
type: 'json',
root: 'data',
},
writer: {
type: 'json',
root: 'data',
},
},
associatinons: [{
type: 'belongsTo',
model: 'My.models.Author',
associationKey: 'Author',
}]
});
由于映射Author.id
上Book
模型({name: 'author_id', mapping: 'Author.id'}
)我能够的author_id
场将作者的数据加载到以下设计用于编辑书籍数据的表格中:
Ext.define('My.views.books.Form', {
extend: 'Ext.form.Panel',
initComponent: function() {
var me = this;
me.items = [{
xtype: 'container',
layout:'anchor',
defaults:{
anchor: '95%'
},
items:[{
xtype: 'textfield',
fieldLabel: 'Book title',
name: 'name'
},{
xtype: 'combo',
fieldLabel: 'Author',
name: 'author_id', // <<-- flat author data
store: ...
}]
}];
me.buttons = [{
text: 'Save',
scope: me,
handler: me.onSave
}];
me.callParent(arguments);
},
...
});
当然还有一个模型de专门为作者指定,但是当我需要将书籍和作者数据一次加载到一个表单中时,我没有看到(现在)其他可能性,如上面显示的那样(当然,请告诉我,如果有更好的解)。
现在的问题:我会从组合中选择一些其他作者,我会保存表格。正如书中记录弄脏(author_id
现在看来为Book
模型场),还有将提高到'My/Books/update'
URL的请求(这是相当好的,我'编辑书)有下列数据发送到服务器:
{
data: {
id: '23',
name: 'JavaScript - The definitive guide',
author_id: '78' // <<-- flat author data
}
}
数据回来服务器不在的,因为他们已被送往ExtJS的,必须做出某种像翻译相同的结构:
$data['Author']['id'] = @$data['author_id'];
unset($data['author_id']);
我问:是否可以治疗由ExtJS的数据以这样一种方式形成,即它们将返回到服务器,只是与它们来的结构相同从服务器?怎么样?
这个问题已经讨论了莫名其妙here,here和here,但既不这些讨论的肯定回答了这个问题。
预先感谢您的帮助! :)