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

℡╲_俬逩灬. 提交于 2019-12-02 20:17:35

From:

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.

Short answer here: _create() will be executed when you run your jquery-ui plugin for the first time, like $xx.your-plugin(your options); _init() will be executed first and after the first time when your code runs into $xx.your-plugin(your options);

As there are some code in jquery-ui.custom.js like this:

var instance = $.data( this, fullName );
if ( instance ) {
    instance.option( options || {} )._init();
}

So, if you draw a chart with jquery-ui plugin, after it's drawn out, then you want to use new data to update it, you need to do this in _init() to update your chart. If you just display something and won't update them totally, _create() will meet your needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!