2012-02-26 7 views
0

jQuery UIの範囲スライダに基づいて電卓を作成しており、その設定が機能しています。私はslideイベントにスライダーをバインドする私のアプリケーションでbindEvents機能を持っています。ここでは、その後jQuery UIの範囲スライダがpubsubイベントに応答しないのはなぜですか?

// this is where I bind all slide events 
    bindEvents: function() { 
     // hold on to the value of this 
     var self = CloudCalc; 

     // bind the slider's slide event to a data event 
     self.sl.on('slide', this.setSliderOutputValue);    
    } 

とは忠実に私のイベントを実行ハンドラです:

setSliderOutputValue: function (event, ui) { 
     // hold on to the value of this 
     var self = CloudCalc; 

     // show the ui output value on slide event 
     self.output.html(ui.value + " INSTANCE"); 
} 

は今、私は複数のイベントをトリガーしますユーザがスライダをスライドさせると、だから私は、そう(addyosmaniののpubsubプラグインを使用して)のように、このセットアップでオブザーバーパターンを使用したいと思います:

bindEvents: function() { 
    // hold on to the value of this 
    var self = CloudCalc; 

    // bind the slider's slide event and publish it 
    self.sl.on('slide', $.publish('slider/moved'));    
} 

ここにサブスクリプションです:

subscriptions: function() { 
    $.subscribe('slider/moved', this.setSliderOutputValue);  
} 

このサブスクリプションは、setSliderOutputValueイベントを呼び出したときに、私を得ますコンソール・エラー・ことわざ:

Uncaught TypeError: Cannot read property 'value' of undefined 

UIプロパティへの参照を意味setSliderOutputValueイベントに渡され取得されていません。

uiプロパティを渡すにはどうすればよいですか?私がこれを間違っていると私を修正してください。

答えて

0

ここで私はpubsubパターンで作業しています。私はこのようなsetSliderOutputValue方法に$.publishの呼び出しを追加しました:

setSliderOutputValue: function (event, ui) { 
     // hold on to the value of this 
     var self = CloudCalc; 

     // show the ui output value on slide event 
     self.output.html(ui.value + " INSTANCE"); 
     $.publish('slider/moved') 
}, 

そして、そのように私のサブスクリプションに追加:

subscriptions: function() { 
     $.subscribe('plan/results', this.parseJSON);  
     $.subscribe('slider/moved', this.setVMval); 
     $.subscribe('slider/moved', this.setRAMval); 
     $.subscribe('slider/moved', this.setHDval);   
}, 

出来上がり!私はに耳を傾けることができ、のイベントは、jQueryの範囲スライダで1つのイベントに基づいてトリガすることができます。

複数の購読呼び出しを1回の呼び出しにリファクタリングすることによってこのコードを改善するための提案はすべて歓迎します。

関連する問題