admin管理员组文章数量:1384656
I have a view that has the html config bound to data within my viewmodel.
The view
Ext.define('MyApp.MyView',{
extend: 'Ext.Panel',
renderTo: Ext.getBody(),
height: 300,
controller: 'myviewcontroller',
viewModel:{
type: 'myviewmodel'
},
title: 'Testing formula updates',
bind:{
html: 'Date: <b>{selectedDate}</b>, <br />Month: <b>{currentMonth}</b>'
},
bbar:[
{xtype: 'button', text: 'Increment Month', handler: 'onIncrementMonth'}
]
});
The ViewModel
Ext.define('MyApp.MyViewModel',{
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.myviewmodel',
data:{
selectedDate: new Date()
},
formulas:{
currentMonth: function (get){
var monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var monthNumber = get('selectedDate').getMonth();
return monthNames[monthNumber];
}
}
});
When the view is initially rendered, the bound data populates into the HTML config as expected.
I then created a method in my viewcontroller to increment the date.
The ViewController
Ext.define('MyApp.MyViewController',{
extend: 'Ext.app.ViewController',
alias: 'controller.myviewcontroller',
onIncrementMonth: function (){
var vm = this.getViewModel();
var dateChange = vm.get('selectedDate');
dateChange.setMonth(dateChange.getMonth() + 1);
vm.set('selectedDate', dateChange);
}
});
I'm sure this is leading to a facepalm moment, but I was expecting that when I update the selectedDate
data in my viewmodel, it should trigger both bound pieces of my html config to update, but it's not.
The viewmodel data is definitely updating. If you inspect the dateChange
variable in the viewcontroller when you click the increment button the second time you'll see that the date has indeed increased.
Is there something I'm missing as far as getting my bound config to update?
I have this here in a fiddle.
I have a view that has the html config bound to data within my viewmodel.
The view
Ext.define('MyApp.MyView',{
extend: 'Ext.Panel',
renderTo: Ext.getBody(),
height: 300,
controller: 'myviewcontroller',
viewModel:{
type: 'myviewmodel'
},
title: 'Testing formula updates',
bind:{
html: 'Date: <b>{selectedDate}</b>, <br />Month: <b>{currentMonth}</b>'
},
bbar:[
{xtype: 'button', text: 'Increment Month', handler: 'onIncrementMonth'}
]
});
The ViewModel
Ext.define('MyApp.MyViewModel',{
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.myviewmodel',
data:{
selectedDate: new Date()
},
formulas:{
currentMonth: function (get){
var monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var monthNumber = get('selectedDate').getMonth();
return monthNames[monthNumber];
}
}
});
When the view is initially rendered, the bound data populates into the HTML config as expected.
I then created a method in my viewcontroller to increment the date.
The ViewController
Ext.define('MyApp.MyViewController',{
extend: 'Ext.app.ViewController',
alias: 'controller.myviewcontroller',
onIncrementMonth: function (){
var vm = this.getViewModel();
var dateChange = vm.get('selectedDate');
dateChange.setMonth(dateChange.getMonth() + 1);
vm.set('selectedDate', dateChange);
}
});
I'm sure this is leading to a facepalm moment, but I was expecting that when I update the selectedDate
data in my viewmodel, it should trigger both bound pieces of my html config to update, but it's not.
The viewmodel data is definitely updating. If you inspect the dateChange
variable in the viewcontroller when you click the increment button the second time you'll see that the date has indeed increased.
Is there something I'm missing as far as getting my bound config to update?
I have this here in a fiddle.
Share Improve this question edited Sep 26, 2015 at 19:02 Tarabass 3,1502 gold badges19 silver badges35 bronze badges asked Sep 26, 2015 at 16:28 Chris SchmitzChris Schmitz 21k31 gold badges90 silver badges154 bronze badges2 Answers
Reset to default 4actually, you need Deep Binding.
ref: http://docs.sencha./extjs/6.0/6.0.0-classic/#!/api/Ext.app.ViewModel.
sometimes you may need to be notified if any of the properties of that object change.
viewModel.bind({
bindTo: '{someObject}',
deep: true
},
function (someObject) {
// called when reference changes or *any* property changes
});
UPDATE: You need Deep Binding as @user2767141 wrote here instead of the ugly workaround I suggested.
Once I changed the line:
vm.set('selectedDate', dateChange);
To:
vm.set('selectedDate', new Date(dateChange));
It works.
本文标签: javascriptExtjs bound html not updating when viewmodel data is changedStack Overflow
版权声明:本文标题:javascript - Extjs bound html not updating when viewmodel data is changed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744458312a2607108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论