javascript 如何在 jQuery UI 小部件中的 _init 和 _create 之间做出决定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4566337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:07:07  来源:igfitidea点击:

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

javascriptjqueryjquery-uijquery-pluginsjquery-ui-plugins

提问by Luke Maurer

I think I understand the difference between _createand _initin widget definitions (see for instance this question), but I'm still not certain about the purposefor the distinction. What sorts of setup tasks go in _create()or in _init()? What goes wrong if the widget author chooses the wrong one?

我想我理解小部件定义之间_create和之间的区别_init(例如参见这个问题),但我仍然不确定区分的目的。什么样的设置任务进入_create()或进入_init()?如果小部件作者选择了错误的,会出现什么问题?

回答by Dave Jarvis

From:

从:

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

使用_create来构建和注射标记,绑定事件,地点等默认功能_init()。例如,对话框小部件提供了一个autoOpen参数,表示在小部件初始化后是否应打开对话框;一个完美的地方 _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...

小部件工厂在初始化期间自动触发_create()_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.

如果作者使用_init()when _create()should have beencoded,结果将是_init()每次widget实例化都会执行一次in的代码。

回答by Franky Yang

Short answerhere: _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 timewhen your code runs into $xx.your-plugin(your options);

简短回答: _create() 将在您第一次运行 jquery-ui 插件时执行,例如 $xx.your-plugin(your options); _init() 将在您的代码第一次运行到 $xx.your-plugin(your options) 之后执行;

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

因为 jquery-ui.custom.js 中有一些这样的代码:

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

So, if you draw a chartwith 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.

所以,如果你用jquery-ui插件绘制图表,绘制出来后,你想用新的数据来更新它,你需要在_init()中这样做来更新你的图表。如果你只是显示一些东西而不会完全更新它们, _create() 将满足你的需求。