問題描述
我想我了解小部件定義中 _create
和 _init
之間的區別(例如,請參閱 this question),但我仍然不確定區分的目的._create()
或 _init()
中有哪些類型的設置任務?如果widget作者選錯了會出現什么問題?
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-開發-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...
如果作者在 _create()
應該被編碼時使用 _init()
,結果將是 _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 之間做出決定?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!