10

プロパティtoおよびfromを持つ日付ピッカー用の ArrayController があります。ユーザーが UI から新しい日付範囲を選択するが変更されます。

したがって、オブザーバーが行き来する関数、日付範囲の 1 回の変更に対して 2 回トリガーされます

日付範囲が変わるたびに関数を1つだけトリガーしたい。残り火でそれを行う方法についての提案

    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')
    });

JS Fiddle で表示

4

2 に答える 2

12

おそらく、 と に計算されたプロパティを導入してfromからto、このプロパティにオブザーバーを接続できます。CP はデフォルトでキャッシュされるため、オブザーバーは 1 回だけトリガーされると思います。

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

dateDidChange: function(){

}.observes('date')

EDITあなたのフィドルのおかげで、setProperties、またはbegin/end propertyChanges http://jsfiddle.net/Sly7/zf2q3/1/を使用して動作するようです

于 2012-11-09T22:48:52.103 に答える
4

オブザーバーをEm.run.once

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

dateDidChange: function() {
  // should be called only once
}
于 2012-11-14T16:58:39.643 に答える