看来我对绑定(bind)时间有一些误解。我有一个简单的组合框,其值绑定(bind)到 View 模型中的某个对象。选择新值,触发更改事件,它在 setValue 方法之后触发,所以我的新值已经设置,但我的 View 模型尚未更新。我的 View 模型什么时候更新?我找到了一些有关调度程序的信息,其中说我需要运行 notify() 方法以立即将更改应用到 viewmodel,但这对我一点帮助都没有。
Ext.define('MyModel', {
extend: 'Ext.data.Model',
idProperty: 'foo',
fields: [{
name: 'bar',
type: 'string'
}]
});
Ext.define('MyViewModel',{
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.my',
data: {
testObj: {
foo: null,
bar: null
}
},
stores:{
combostore: {
model: 'MyModel',
data: [{
foo: '1',
bar: 'qwerty'
},{
foo: '2',
bar: 'ytrewq'
}]
}
}
});
Ext.define('MyViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.my',
onChange: function() {
var vm = this.getViewModel();
vm.notify();
console.log(vm.get('testObj.foo'));//supposed to be current value
}
});
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.create('Ext.container.Viewport', {
controller: 'my',
viewModel: {
type: 'my'
},
layout : 'vbox',
items : [
{
xtype : 'combo',
valueField: 'foo',
displayField: 'bar',
queryMode: 'local',
bind: {
store: '{combostore}',
value: '{testObj.foo}'
},
listeners:{
change: 'onChange'
}
}
]
});
}
});
还有 fiddle :https://fiddle.sencha.com/#fiddle/r88
最佳答案
我知道这是一个迟到的回应,但我认为这个问题仍然相关。看到这个 fiddle :https://fiddle.sencha.com/#fiddle/2l6m&view/editor .
本质上,这个想法是您监听绑定(bind)变量的变化,而不是监听组合框选择的变化(这会触发绑定(bind)变量的重新评估)。
关键代码在我为 View 模型添加的构造函数中:
Ext.define('MyViewModel',{
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.my',
// added this to enable the binding
constructor: function () {
var me = this;
me.callParent(arguments);
me.bind('{selectedItem}', function (value) {
console.log('combobox selected item changed (bar value): ' + (value === null ? "null": value.get('bar')));
console.log(me.getView().getController());
});
me.bind('{testObj.foo}', function (value) {
console.log('combobox value (foo value): ' + value);
// you can access the controller
console.log(me.getView().getController());
});
},
data: {
testObj: {
foo: null,
bar: null
},
selectedItem: null,
},
stores:{
combostore: {
model: 'MyModel',
data: [{
foo: '1',
bar: 'qwerty'
},{
foo: '2',
bar: 'ytrewq'
}]
}
}
});
这是 View (注意绑定(bind)到选择):
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.create('Ext.container.Viewport', {
controller: 'my',
viewModel: {
type: 'my'
},
layout : 'vbox',
items : [
{
xtype : 'combo',
valueField: 'foo',
displayField: 'bar',
queryMode: 'local',
bind: {
store: '{combostore}',
value: '{testObj.foo}',
selection: '{selectedItem}'
},
listeners:{
change: 'onChange'
}
}
]
});
}
});
绑定(bind)选择不是必需的,但我还是将其作为另一个选项包含在内,因为有时您可能希望在绑定(bind)到列表的存储中存储额外的数据。
我希望这对其他人有帮助。
关于javascript - ExtJS 5.1.1 立即在 ViewModel 中触发绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682264/