我有两个型号:书属于关联作者。 我们的调查与图书数据一起发送的道路上从服务器到ExtJS的和后面的作者的数据。 服务器将嵌套的数据如下,以ExtJS的:
{
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
上author_id
领域Book
模型( {name: 'author_id', mapping: '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);
},
...
});
当然有一个模型专门创作,但是当我需要的书和作者的数据加载到一种形式的一次,我没有看到(暂时)另一种可能性,如上图所示(当然的一个告诉我的话,请,如果有一些更好的解决方案)。
现在的问题来了:我会选择从组合一些其他作家,我会保存表单。 正如书中记录弄脏( 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的表单数据? 怎么样?
这个问题已经讨论了不知何故在这里 , 这里和这里 ,但既不这些讨论的肯定回答了这个问题。
预先感谢帮忙! :)