How to decide between _init and _create in jQuery UI widget?

后端 未结 2 1780
傲寒
傲寒 2021-02-01 16:23

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

2条回答
  •  借酒劲吻你
    2021-02-01 17:15

    From:

    • 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/

    Use _create to build and inject markup, bind events, etc. Place default functionality in _init(). The dialog widget, for example, provides an autoOpen parameter denoting whether or not the dialog should be open once the widget is initialized; a perfect spot for _init()!

    Also:

    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...

    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.

提交回复
热议问题