虽然这个问题得到了回答,但我认为可以通过两种方式以不同的方式(更简洁)完成。
首先是 CD 使用的方式,这是一个很棒的答案,但更简洁,在您的 controller 中没有任何逻辑。让viewmodel 完成他的工作:
将bind 属性配置为grid 上的selection:
bind: {
selection: '{rec}'
},
字段保持不变:
items: [{
xtype: 'textfield',
fieldLabel: 'Firstname',
bind: '{rec.firstName}'
}, {
xtype: 'textfield',
fieldLabel: 'Lastname',
bind: '{rec.lastName}'
}]
就是这样。现在您可以删除窗口中的逻辑controller。
工作示例:https://fiddle.sencha.com/#fiddle/ulf
第二种方式,我经常使用这种方式,是在您的viewmodel 上使用deep binding。这是为了跟踪选择的record,无论它发生了什么变化或在哪里发生了变化。这可以通过bind 和deep: true 来完成。
在您的(单独的)viewmodel 中放置一个formula:
formulas: {
rec: {
// We need to bind deep to be notified on each model change
bind: {
bindTo: '{myGrid.selection}', //--> reference configurated on the grid view (reference: myGrid)
deep: true
},
get: function(record) {
return record;
},
set: function(record) {
if(!record.isModel) {
record = this.get('records').getById(record);
}
this.set('currentRecord', record);
}
}
}