2010-03-09 64 views

回答

1

我相信這樣的事情可能的工作,雖然我沒有測試它:

if (yourWidget.domNode) { 
    // here your widget has been rendered, but not necessarily its child widgets 
} else { 
    // here the domNode hasn't been defined yet, so the widget is not ready 
} 

Dijit的小部件繪製是通過擴展點來處理,稱爲按順序:

  1. postMixinProperties
  2. buildRendering
  3. postCreate < ==此時,您的小部件已經變成HTML並插入到頁面中,你可以訪問像this.domNode這樣的屬性。然而,沒有一個子控件已經照顧
  4. 啓動的:這就是所謂的,所有的子控件已經制定

後的最後一個擴展點(這是小部件的生命週期上的解釋「掌握道場」)。

例:

<html> 
<head> 
<script src="path/to/your/dojo/dojo.js" djConfig="parseOnLoad: true, isDebug: true"></script> 
<script type="text/javascript"> 
    dojo.require("dojo.parser"); 
    dojo.require("dojox.lang.aspect"); 
    dojo.require("dijit.form.Button"); 

    // Define your aspects 
    var startupAspect = { 
      before : function() {console.debug("About to execute the startup extension point");}, 
      after : function() {console.debug("Finished invoking the startup extension point");}, 
    }; 

    function traceWidget(theWidget) { 
     // Attach the aspect to the advised method 
     dojox.lang.aspect.advise(theWidget, "startup", startupAspect); 
    } 

</script> 

</head> 
<body> 
<button dojoType="dijit.form.Button" type=button"> 
      dijitWidget 
    <script type="dojo/method" event="postCreate"> 
     traceWidget(this); 
    </script> 
    <script type="dojo/method" event="startup"> 
     console.debug("Inside startup"); 
    </script>  
    </button> 
</body> 
</html> 
+0

謝謝!我得到儘可能postCreate,看起來像我「啓動」是我正在尋找的回調。我會去嘗試一下,看看我能看到什麼。 「掌握Dojo」是一本很棒的書,但是,我仍然發現它缺少一些關鍵信息。但是,使用像dojo這樣廣泛的JS框架,我相信很難將所有信息都整合到一本書中。 – 2010-03-10 13:01:00

+0

如果要測試的是啓動擴展點已執行或未執行,maybie可以嘗試AOP(請參閱http://lazutkin.com/blog/2008/may/18/aop-aspect-javascript -dojo /和http://www.dojotoolkit.org/api/dojox/lang/oo/aop.html)。我會嘗試用「之後」或「返回後」的建議 – Philippe 2010-03-10 13:06:45

+1

哇...我想我每天都越來越喜歡道場... – 2010-03-10 13:10:57

3

我相信如果設置了dijit的「domNode」屬性,則創建了該窗口小部件的DOM。該小部件可能會或可能不會連接到較大的DOM,這可能是一個單獨的步驟。檢查domNode.parentNode是一個真正的元素可能會有所幫助,但不能保證parentNode也處於活動文檔中。

相關問題