2010-03-09 59 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也处于活动文档中。

相关问题