2016-10-20 6 views
0

私は、別のAMDjsユニット内で宣言され、定義されたオブジェクトのメソッドを呼び出そうとしています。別のAMDjsユニットからオブジェクトのメソッドを呼び出す?

最初単位:

define([ .. 'XTree' ], function(.. xTree) 
 
{ 
 
    ... 
 
    
 
    var btnX = document.createElement('input'); 
 
     btnX.type = 'button'; 
 
     btnX.value = 'hey'; 
 
     btnX.style.width = '90px'; 
 
     btnX.style.height = '25px'; 
 
     btnX.onclick = function () { xTree.fire() }; 
 
    ... 
 
});

    require(
     
    [ 
     
        "dojo/ready", 
     
        "dojo/store/Memory", \t \t \t \t 
     
        "cbtree/Tree", \t \t \t \t \t \t 
     
        "cbtree/model/TreeStoreModel", \t 
     
        "dojo/dom-class", 
     
        "dojo/domReady", 
     
        "dijit/form/CheckBox", 
     
        "dojo/domReady!" 
     
    ], 
     
    function 
     
    (
     
        ready, 
     
        Memory, 
     
        Tree, 
     
        ObjectStoreModel, 
     
        domClass, 
     
        CheckBox 
     
    ) 
     
    { 
     
    
     
        var XTree = function() 
     
        { 
     
         this.store = new Memory({ data: this.datax() }); 
     
    
     
         this.model = new ObjectStoreModel(
     
         { 
     
          store: this.store, 
     
          query: { id: "all" }, 
     
          rootLabel: "xxx", 
     
          checkedRoot: true 
     
         }); 
     
        } 
     
    
     
        XTree.prototype.applyTheTree = function() 
     
        {     
     
         this.tree = new Tree({ model: this.model, id: "tree00", animation: false }, "treeViewx"); 
     
         this.tree.startup(); 
     
        } 
     
    
     
        XTree.prototype.fire = function() 
     
        { 
     
         alert('fire'); 
     
        } 
     
    
     
        XTree.prototype.datax = function() 
     
        { 
     
         return [ 
     
    
     
            { id: 'all', name:'' }, 
     
             { id: 'br1', name:'branch 1', parent: 'all' }, 
     
              { id: '1', name:'b 1', parent: 'br1' }, 
     
            { id: '1', name:'b 2', parent: 'all' } 
     
           
     
           ] 
     
        }; 
     
        
     
    
     
        var xTree = new XTree(); 
     
        
     
    
     
        ready(function() 
     
        { 
     
         xTree.applyTheTree(); 
     
           
     
        }); 
     
    
     
        return xTree; 
     
        
     
    });

    Iが第二ユニット内でこのような別の装置からオブジェクトを参照しようとここ は、例えば、コードです

  • ボタンをクリックする理由結果x fire()メソッドをトリガーするのではなく、ツリーが定義されていませんか?

答えて

0

requiredefineは逆です。

defineを使用して、他のコードで使用できるモジュールを定義します。一般に、defineはjavascriptファイルで使用されます。

モジュールを定義していないが、定義済みのモジュールが必要な場合は、requireを使用します。一般的に、requireはHTMLページで使用されます。 HTMLページはモジュールではありませんが、ページをユーザーに提示するためのモジュールが必要です。

また、dojoを使用してプロトタイプを使用しないようにしてください。 'dojo/_base/declare'を使用してモジュールコンストラクタを作成することができます。

define([ "dojo/ready", 
    "dojo/_base/declare", 
    "dojo/store/Memory",    
    "cbtree/Tree",       
    "cbtree/model/TreeStoreModel",  
    "dojo/dom-class", 
    "dojo/domReady", 
    "dijit/form/CheckBox", 
    "dojo/domReady!" 
], function(ready, 
    declare, 
    Memory, 
    Tree, 
    ObjectStoreModel, 
    domClass, 
    CheckBox){ 


    var XTree = declare(null, { 

     constructor: fuction(){ 
      this.store = new Memory({ data: this.datax() }); 
      this.model = new ObjectStoreModel({ 
       store: this.store, 
       query: { id: "all" }, 
       rootLabel: "xxx", 
       checkedRoot: true 
      }); 
     }, 

     applyTheTree : function(){     
      this.tree = new Tree({ model: this.model, id: "tree00", animation: false }, "treeViewx"); 
      this.tree.startup(); 
     }, 

     fire : function(){ 
      alert('fire'); 
     }, 

     datax = function(){ 
      return [{ id: 'all', name:'' }, 
       { id: 'br1', name:'branch 1', parent: 'all' }, 
       { id: '1', name:'b 1', parent: 'br1' }, 
       { id: '1', name:'b 2', parent: 'all' }] 
     } 
    }); 

    var xTree = new XTree(); 
    ready(function(){ 
     xTree.applyTheTree(); 
    }); 

    return xTree; 
}); 

ここでは、コンストラクタではなくオブジェクトのインスタンスを返します。基本的に、シングルトンモジュールを作成しようとしています。あれは正しいですか?

また、別のモジュールまたは必要なメソッドを必要としている間にモジュールパスが正しいことを確認してください。コンソールでUndefinded Module例外が発生した場合は、別の問題があります。

希望しました。

+0

何らかの理由で似ているようですが、現時点では私の二次モジュールのユニットを参照しています。それはコンストラクタをうまく通過します。 –

+0

欠けている部分はシングルトンそのものでした。魅力的な作品! –

関連する問題