0

私は自分の ember.js アプリでカスタム jQuery UI プラグインを使用しており、コントローラー/モデルの値を更新するためにカスタム jQuery イベントに反応したいと考えています。

私が見つけたもの:スタックオーバーフローで同じ目的のために尋ねられた質問は、ビューのdidInsertElement関数でjQuery.bind()/[jQuery.on() を使用する答えとして与えます。ビューのdidInsertElement -Method でjQuery.on()を使用すると、イベント Handler 内のthisが発生した Dom-Element にバインドされるという問題が発生します。

したがって、理想的には、他の jQuery イベントを ember がもたらすものと同じように動作させる方法があるでしょう。つまり、Ember.View-Instance で一致するメソッド名を呼び出します。

カスタム jQuery イベントを機能させる方法を知っていますか? おそらく、ビューなどで適切なメソッドを作成するためにMixinを使用していますか?

4

1 に答える 1

1

これは、jQuery や Ember の問題ではなく、Javascript の問題のようです。「this」が何にバインドされているかを理解し、操作することは、Javascript の最大の学習曲線の 1 つです。

///...in the view
didInsertElement : function(){
    var view = this;
    this.$().zoomablebg(); //this is the name of the custom jQ UI-Plugin
    // the view variable now holds a reference to the ember view
    // and creates a closure so that you can refer to it in the event handler
    this.$().on("zoomablebgstopdrag", function(e){ view.zoomablebgstopdrag(e) });
},
zoomablebgstopdrag: function(b){
    console.log(this); //this refers to the view now. 
},
//more view code

それが役立つことを願っています!

于 2013-04-19T12:36:58.237 に答える