Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide between _init and _create in jQuery UI 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?

like image 584
Luke Maurer Avatar asked Dec 30 '10 21:12

Luke Maurer


People also ask

How is the default formatting for a jQuery UI widget done?

To change the defaults, you can use the event, collapsible, heightStyle, and animated options. The basic formatting of the accordion is done by the CSS for jQuery UI, but you can use CSS to format the contents within the panels.

Which are jQuery ul widgets?

a jQuery UI widget is a specialized jQuery plug-in. Using plug-in, we can apply behaviours to the elements. However, plug-ins lack some built-in capabilities, such as a way to associate data with its elements, expose methods, merge options with defaults, and control the plug-in's lifetime.

What is a widget factory?

The widget factory defines how to create and destroy widgets, get and set options, invoke methods, and listen to events triggered by the widget. By using the widget factory to build your stateful plugins, you are automatically conforming to a defined standard, making it easier for new users to start using your plugins.


2 Answers

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.

like image 151
Dave Jarvis Avatar answered Sep 19 '22 11:09

Dave Jarvis


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.

like image 44
Franky Yang Avatar answered Sep 19 '22 11:09

Franky Yang