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

后端 未结 2 1793
傲寒
傲寒 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:07

    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.

提交回复
热议问题