我从我的 JS 代码创建和dijit.layout.ContentPane
的实例。dijit.layout.StackContainer
dijit.layout.BorderContainer
看来我必须调用以startup()
编程方式创建的实例的方法。但是,我不确定我必须为每个小部件调用它。例如,当我执行 ' new my.foo.widget()
' 时,startup()
会自动触发。
感谢您帮助我了解何时调用该startup()
方法!
startup() 在 _Widget 中定义,只是“生命周期的一部分”。这是小部件生命周期的最后一步,并非所有小部件都需要。绝对需要它的最常见情况是在以编程方式创建布局小部件时。当孩子需要调整大小时,它被用作防止冗余计算的一种方法。例如,一个 BorderContainer。
var bc = new dijit.layout.BorderContainer({
style:"height:200px; width:200px"
});
// can call bc.startup() now, and the BorderContainer will resize
// all children each time a new child is added. Or, we can add all
// our children now, then trigger startup() and do it all at once.
var top = new dijit.layout.ContentPane({
region:"top", style:"height:100px"
}).placeAt(bc);
var mid = new dijit.layout.ContentPane({ region:"center" }).placeAt(bc);
// now BC will do the calculations, rather than in between each
// the above addChild/placeAt calls.
bc.startup();
在 parseOnLoad:true 或手动执行的情况下,解析器会自动调用 Startup。解析器延迟调用 startup() 直到所有找到的子窗口部件都被适当地实例化。
dijit.Dialog 是一个奇怪的案例。startup() 也必须在这个小部件上调用。
var dialog = new dijit.Dialog({ title:"Hmm", href:"foo.html" });
dialog.startup();
dialog.show();
大多数小部件不需要启动调用,但是在从 _Widget 继承的某些内容不覆盖启动成员的情况下,调用本质上是一个无操作设置 this._started = true; 如果您创建自己的 startup() 函数,您应该调用 this.inherited(arguments) 或简单地手动设置 _started 触发器。
在 Dojo 1.4 中,这里的生命周期进行了轻微调整。以前,带有 widgetsInTemplate:true 的小部件会在父小部件上调用 startup() 之前在子小部件上调用 startup()。在 1.4 中,子代的 startup() 将在父代的 startup() 之后调用。这种行为是递归的,但是实例化了带有 widgetsInTemplate:true 的许多级别的嵌套小部件。
调用 .startup() 始终是“安全的”,但如果您“知道”(因为它是一个简单的端点小部件或您自己的自定义 _Widget 代码),您可以省略该调用。
您确定它是自动调用的,还是您的小部件中有调用它的代码?
调用它是一个好习惯,因为它设置_started
了true
很多小部件使用它来确定行为和/或布局。大多数小部件都要求您调用它(如您所见)。
startup
被需要控制子小部件的复合小部件大量使用。
基本上,任何继承自的东西都dijit._Widget
应该在实例化后调用启动。
编辑:
SitePen 上有一篇关于dijit生命周期的文章,在文章本身和评论中讨论了启动。不过,它已经有两年多了,我认为情况已经发生了变化。
O'Reilly dojo的书也谈到了启动,但它说应该在容器小部件上调用它。我不得不在网格上调用它,所以这也没有意义。
调用它并没有真正伤害任何东西(除非您在将孩子添加到您的小部件之前调用它)。我会说总是打电话。