Ember多个属性更改,但希望触发单个事件 [英] Ember multiple property changes but want to trigger single event
问题描述
我有一个ArrayController,用于日期选择器,其属性至和 。现在,当用户从的到和值之间的用户选择新的日期范围变化时。
所以在至和之间观察的功能在日期范围内的单次更改会触发两次。 / p>
我想要触发功能,每个日期范围变化只有一个。任何建议如何做到这一点与ember
app = Ember.Application.create();
app.Time = Ember.ArrayController.create({
to:null,
from:null,
rootElement:#time
debug1:function(){
this.set('to',1);
this.set('from',2);
}
debug2:function(){
this.setProperties({
'to':3,
'from':4
});
},
debug3:function(){
this.beginPropertyChanges();
this.set('to',5);
this。 set('from',6);
this.endPropertyChanges();
},
search:function(){
alert('called');
} .observes('to','from')
});
也许您可以从 >和到
,然后在此属性上插入观察者。由于CP默认缓存,我认为观察者将被触发一次。
date:function(){
//返回计算日期
} .property('from','to')
dateDidChange:function(){
} .observes('日期')
编辑感谢您的小提琴,似乎可以使用setProperties或开始/结束属性更改 http://jsfiddle.net/Sly7/zf2q3/1/
I have a ArrayController for date picker with properties to and from. Now when user selects a new date range from the UI both to and from value changes.
So a function which observers on to and from is trigger twice for a single change in date range.
I want to trigger the function only one every date range change. Any suggestions how to do that with ember
app = Ember.Application.create();
app.Time = Ember.ArrayController.create({
to: null,
from: null,
rootElement: "#time",
debug1: function() {
this.set('to', 1);
this.set('from', 2);
},
debug2: function() {
this.setProperties( {
'to': 3,
'from': 4
});
},
debug3: function() {
this.beginPropertyChanges();
this.set('to', 5);
this.set('from', 6);
this.endPropertyChanges();
},
search: function() {
alert('called');
}.observes('to', 'from')
});
Perhaps you can introduce a computed property on from
and to
, and then plug the observer on this property. As CPs are cached by default, I think the observer will be triggered only once.
date: function(){
// return the computed date
}.property('from', 'to')
dateDidChange: function(){
}.observes('date')
EDIT Thanks to your fiddle, it seems to work using setProperties, or begin/end propertyChanges http://jsfiddle.net/Sly7/zf2q3/1/
这篇关于Ember多个属性更改,但希望触发单个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!