2017-02-22 15 views
1

drawSectionと呼ばれるとき誰も私に説明することができます 'this'の値は、グローバルスコープですか?dojoが必要とスコープ

ウィジェットを失う前に別の変数にウィジェットを保存しなくても、ここで使用する必要がありますか?

define("my/TextBox", [ 
    "dojo/_base/declare", 
    "dijit/form/ValidationTextBox" 
], function(
declare, ValidationTextBox 
) { 

    function drawSection() { 
     alert(this); 
     require(["dojo/dom-construct"], function(domConstruct) { 
     alert(this); // this = window 
     }); 
    };  

    return declare([ValidationTextBox], { 
     postCreate: function() { 
      this.inherited(arguments);    
      drawSection.call(this) 
     } 
    }); 
}); 

答えて

2

それは問題を解決するための簡単な使用dojo/_base/langhitch()機能を終了しています。

require(["dojo/dom-construct"], function(domConstruct) {....})内部機能は、グローバルコンテキストを参照のうえているので、

ので(thisを使用して)現在のコンテキストでlang.hitch関数を使用しproblemeが解決される。ここで

Fiddle

と上の作業スニペット:

define("my/TextBox", [ 
 
\t "dojo/_base/lang", 
 
    "dojo/_base/declare", 
 
    "dijit/form/ValidationTextBox" 
 
], function(lang, 
 
    declare, ValidationTextBox 
 
) { 
 

 
    function drawSection() { 
 

 
    alert(this); 
 

 
    require(["dojo/dom-construct"], lang.hitch(this,function(domConstruct) { 
 

 
     alert(this); // this = window 
 

 
    })); 
 

 
    }; 
 
    return declare([ValidationTextBox], { 
 
    postCreate: function() { 
 
     this.inherited(arguments); 
 
     drawSection.call(this) 
 
    } 
 
    }); 
 
    
 
}); 
 

 

 
require([ 
 
    "dojo/parser", 
 
    "my/TextBox", 
 
    "dojo/domReady!" 
 
], function(
 
    parser, 
 
    TextBox 
 
) { 
 
    
 
    // important: parse document after the ValidationTextBox was extended 
 
    parser.parse(); 
 
    
 
});
<link href="https://ajax.googleapis.com/ajax/libs/dojo/1.8/dijit/themes/claro/claro.css" rel="stylesheet"/> 
 
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script> 
 

 
<body class="claro"> 
 
<input type="text" data-dojo-type="my/TextBox" />, 
 
</body>

+0

なぜこんなにグローバルスコープに戻っていますか? – blu10

+1

呼び出し側が定義したスコープ( 'require')を' window'とします。 – ben

+0

ベンが言ったように、 'lang.hitch(this、function(){})'は 'this'の関数を実行することを意味します。 'lang.hitch(window、function(){}) 'がクラスではなく' window'のスコープで実行されると宣言すれば、現在のクラスを参照してください。 –

0

あなたはdojo/_base/langlang.hitch次のように使用する必要があります。

共通の閉鎖問題だ
require(["dojo/dom-construct"], lang.hitch(this, function(domConstruct) { 

     alert(this); // this = window 

    })); 


が必要「オンデマンド」ので、あなたはpostCreateからそれを呼び出すように、私はウィジェット内部drawSection方法を有し、かつ上に必要なdom-construct推薦(あなたは常にそれを必要とします優れたプラクティスとしてhttps://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#hitch

をされて参照してください。オーバーキル)

define("my/TextBox", [ 
     "dojo/_base/declare", 
     "dijit/form/ValidationTextBox", 
     "dojo/dom-construct" 
    ], function(declare, ValidationTextBox, domConstruct) { 

    return declare([ValidationTextBox], { 
     postCreate: function() { 
       this.inherited(arguments);    
       this.drawSection() 
     }, 
     drawSection: function() { 
       alert(this); 
       //domConstruct.whaever you want 
     }; 
    }); 
    }); 
関連する問題