2016-07-23 13 views
-1

名前がTestClassのクラスがあります。それはTooltipDialogオブジェクトメンバーとtestFunc関数を持っています。
ToolTipDialogには、ラジオボタンがあります。
ユーザーがラジオボタンをクリックすると、testFuncが実行されることが予想されます。
質問:私がラジオボタンをクリックしたときに、親コンテキスト(TestClass)を取得するにはどうすればよいですか? TestClass.testFunc()のようなものを実行する必要があります。 iはtestFuncと試みたとき
は、エラーがあった:testFunc is not defined
iはthis.testFuncと試みた場合、エラーがTestClassのコードは以下の通りであるthis.testFunc is not a functionJavaScriptのオブジェクトの正しいコンテキストを取得する方法

た、フィドルリンクはjs code

define(["dojo/_base/declare","dojo/dom","dojo/on","dojo/_base/lang","dijit/registry", 
     "dijit/TooltipDialog","dijit/popup","esri/InfoWindowBase", 
     "dojo/_base/array"], 
    function (declare,dom,on,lang,registry,TooltipDialog,popup,InfoWindowBase,Array) { 
    return declare( 
     InfoWindowBase, 
       { 
      constructor: function (parameters) { 
       //lang.mixin(this, parameters); 
       this.son = ""; 
      }, 
      //member of the ParentClass 
      init: function () { 
       var tpContent = '<div id="dp1"><label for="name">first:</label> <input type="radio" id="first" checked value="pri"/>' + 
        '<br><label for="hobby"> second:</label> <input type="radio" id="second" value="sec"/></div>'; 

       this.inherited(arguments); 
       this.son = new TooltipDialog({ 
        content: tpContent, 
        onMouseLeave: function (e) { 

        } 
       , 
        onOpen: function (e) { 
        var sHu=dom.byId("first"); 
         on(sHu,"click", function(){ 
          // this.testFunc(); 
         }); 

        }, 

       }); 

       popup.open({ 
        popup: this.son, 
        orient: ["above","below"], 
        around: dom.byId("tpHolder") 
       }); 

      }, 
      testFunc:function(){ 
       console.log("testFunc"); 
      } 
     } 
    ); 

}); 
+0

質問は何ですか? [Ask]を確認してください – charlietfl

答えて

0

@Tカンビは、hitchを使用する必要があると言いました。 しかし、あなたの場合、あなたは二重のhitchが必要になります。

onOpen: lang.hitch(this, function (e) { 
    var sHu=dom.byId("first"); 
    on(sHu,"click", lang.hitch(this, function(){ 
      this.testFunc(); 
    })); 
}), 

最初hitchthisonOpen方法でTestClassするようになります、第二hitchonOpen方法のthisはのthisことになりますclickハンドラ。

私はJSFiddleを固定するので、あなたが見ている:https://fiddle.jshell.net/etxx1o0s/5/

+0

ありがとうございました。 @ben、@Tカンビ –

0

ありますイベントのコンテキストは、イベントが発生しているコントロール/ノードになります。その場合は、オブジェクトになります:sHu。コンテキストを変更するには、lang.hitchメソッドを使用します。

on(sHu,"click", lang.hitch(this, function(){ 
    this.testFunc(); 
})); 

あなたが関数のコンテキストとしてたい任意のオブジェクトに変更することができthisを、合格する必要はありません。

+0

こんにちは@カンビ、私は上記のようにlang.hitchを試してみました。ここで、 "this"は "Object {id:dijit_TooltipDialog_0}"を指しています。ここでは "TestClass"を指す必要があります。私はTestClassオブジェクトを保持する方法を知らない。 –

関連する問題