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 代码),您可以省略该调用。