如何在jQuery UI小部件中决定_init和_create? [英] How to decide between _init and _create in jQuery UI widget?
问题描述
我想我理解小部件定义中 _create
和 _init
之间的区别(例如参见这个问题),但我仍然不确定目的区别。什么类型的设置任务进入 _create()
或 _init()
?如果小部件作者选择了错误的,会出现什么问题?
I think I understand the difference between _create
and _init
in widget definitions (see for instance this question), but I'm still not certain about the purpose for the distinction. What sorts of setup tasks go in _create()
or in _init()
? What goes wrong if the widget author chooses the wrong one?
推荐答案
来自:
- http://forum.jquery .com / topic / jquery-ui-1-8-use-of-init
- http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/
- http://jqueryui.com/demos/dialog/
- http://forum.jquery.com/topic/jquery-ui-1-8-use-of-init
- http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/
- http://jqueryui.com/demos/dialog/
使用
_create
构建并注入
标记,绑定事件等。放置
_init()
中的默认功能。
对话框小部件,例如
,提供autoOpen
参数
表示对话框
是否应该打开一次小部件是
初始化;
的理想位置_init()
!
Use
_create
to build and inject markup, bind events, etc. Place default functionality in_init()
. The dialog widget, for example, provides anautoOpen
parameter denoting whether or not the dialog should be open once the widget is initialized; a perfect spot for_init()
!
另外:
小部件工厂自动触发
_create()
和_init()
方法
在初始化期间按此顺序排列。
乍一看似乎
的努力是重复的,但两者之间存在
的视线差异。
因为小部件工厂保护
免受
相同元素上的多个实例化,_create()
将被称为
最大值每个
小部件实例一次,而_init()
每次小部件是
时调用
不带参数调用.. 。
The widget factory automatically fires the
_create()
and_init()
methods during initialization, in that order. At first glance it appears that the effort is duplicated, but there is a sight difference between the two. Because the widget factory protects against multiple instantiations on the same element,_create()
will be called a maximum of one time for each widget instance, whereas_init()
will be called each time the widget is called without arguments...
如果作者在 _init()
> _create()应该已经编码,结果是每个小部件实例化将执行一次 _init()
中的代码。
If the author uses _init()
when _create()
should have been coded, the result will be that the code in _init()
will be executed once per widget instantiation.
这篇关于如何在jQuery UI小部件中决定_init和_create?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!