gpt4 book ai didi

layout - 我什么时候必须调用以编程方式实例化的小部件的 startup() 方法?

转载 作者:行者123 更新时间:2023-12-04 05:27:40 25 4
gpt4 key购买 nike

我创建了 dijit.layout.ContentPane 的实例, dijit.layout.StackContainerdijit.layout.BorderContainer从我的 JS 代码。

看来我得调用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();

Startup 在 parseOnLoad:true 或手动执行的情况下由解析器自动调用。解析器延迟调用 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 中, child 的启动()将在父启动()之后被调用。无论实例化具有 widgetsInTemplate:true 的嵌套小部件的多个级别,此行为都是递归的。

调用 .startup() 总是“安全的”,但如果你“知道”(因为它是一个简单的端点小部件,或者你自己的自定义 _Widget 代码),你可以省略调用。

关于layout - 我什么时候必须调用以编程方式实例化的小部件的 startup() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1289565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com