我有一个用于日期选择器的ArrayController,其属性到和的。现在,当用户从用户界面中选择新的日期范围时,到以及的值都会更改。
因此,观察者从到和到和的函数针对日期范围的单个更改触发两次。
我只想在每个日期范围更改一次触发该功能。关于如何用 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')
});
View in JS Fiddle
最佳答案
也许您可以在from
和to
上引入一个计算属性,然后将观察者插入该属性。由于默认情况下会缓存CP,因此我认为观察者只会被触发一次。
date: function(){
// return the computed date
}.property('from', 'to')
dateDidChange: function(){
}.observes('date')
编辑多亏了您的 fiddle ,它似乎可以使用setProperties或begin/end属性更改http://jsfiddle.net/Sly7/zf2q3/1/
关于properties - Ember 多个属性更改,但要触发单个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316538/