javascript 使用 onShow 和 onLoad 为 dijit.layout.ContentPane 创建程序化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4626838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:20:53  来源:igfitidea点击:

using onShow and onLoad for dijit.layout.ContentPane created programatic

javascriptdojo

提问by noru

I have a ContentPane created both declarative and programmatic.

我有一个 ContentPane 创建了声明性和程序性。

Declarative :

声明:

<div dojoType="dijit.layout.ContentPane" id="abccp" href="abc.php?id=1" title="abc" onShow="do_abc()">

Programmatic

程序化

var obj_abc;
var abchref= "abc.php?id=1";
obj_abc = new dijit.layout.ContentPane({id:'abccp',title:'abc', href:abchref});

How can I call do_abc() in the programmatic ex

如何在程序化前调用 do_abc()

回答by Ken Franqueiro

To be technically equivalent to your first example, you'd just include onShow: do_abcwithin the arguments object passed to ContentPane's constructor. (Note no parentheses after do_abc- we're interested in the function object itself, not the result of calling it!)

为了在技术上等同于您的第一个示例,您只需onShow: do_abc在传递给 ContentPane 构造函数的参数对象中包含。(注意后面没有括号do_abc——我们对函数对象本身感兴趣,而不是调用它的结果!)

However, if you'd like to do it in a bit more extensible of a fashion, then I'd suggest doing it like this:

但是,如果您想以更具可扩展性的方式进行操作,那么我建议您这样做:

obj_abc = new dijit.layout.ContentPane(...);
obj_abc.connect(obj_abc, 'onShow', do_abc);

What this does is perform a hookup such that whenever obj_abc's onShowmethod is called, the do_abcfunction will in turn be called (though in the context of obj_abc, which presumably is what you want anyway). You also get the following added bonuses:

它的作用是执行一个连接,这样无论何时调用obj_abconShow方法,该do_abc函数都会被调用(尽管在 的上下文中obj_abc,这大概是您想要的)。您还将获得以下额外奖励:

  • It no longer clobbers any default functionality that might be originally present in the method (though in this case, onShowis a stub meant to be clobber-able)
  • You can connect any number of functions to onShowin this way
  • The connection will automatically be torn down when the widget is destroyed (as opposed to dojo.connectwhich you would have to tear down manually).
  • 它不再破坏方法中最初可能存在的任何默认功能(尽管在这种情况下,它onShow是一个可以破坏的存根)
  • 您可以通过onShow这种方式连接任意数量的功能
  • 当小部件被销毁时,连接将自动断开(与dojo.connect您必须手动拆卸相反)。

For more information:

想要查询更多的信息: