2012-11-09 55 views
9

我有一个数组控制器的日期选取器的属性。现在当用户从UI中选择一个新的日期范围值更改。Ember多个属性更改,但想要触发单个事件

所以从上观察员被触发两次在日期范围内的单个变化的函数。

我想触发函数只有每个日期范围更改。任何建议,如何做到这一点与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

+0

包裹观察员大约只观察到了什么或从?因为当用户选择nex日期时,两者都在变化,只依靠其中的一个可以不工作? –

+0

以及还有其他方法来改变往返也。所以观察员必须在 –

+0

看到我编辑的答案:) –

回答

11

也许你可以在fromto引入计算财产,然后再插上观察者这个属性。由于CP默认缓存,我认为观察者只会被触发一次。

date: function(){ 
// return the computed date 
}.property('from', 'to') 

dateDidChange: function(){ 

}.observes('date') 

编辑感谢您的小提琴,似乎使用setProperties方法结束propertyChanges工作,或开始/ http://jsfiddle.net/Sly7/zf2q3/1/

4

您可以在Em.run.once

_dateDidChange: function(){ 
    Ember.run.once(this, 'dateDidChange'); 
}.observes('from', 'to'), 

dateDidChange: function() { 
    // should be called only once 
} 
+0

我不认为它的好办法做到这一点。 –

+0

@AnkurAgarwal嗯,我认为这是我做的更好。事实上,我已经要求Ember.js的贡献者看看这个。这就是为什么他建议采取其他解决方法。 –

+0

Ankur,你能推断吗?我们正在考虑让观察者异步,所以你不必包装它,这将是正确的工作。 – Tchak

相关问题