9

Some jquery plugin extend widget use _create method, while others use _init method, can someone explain the differences between the two?

Also any guidance on when it is better to extend widget or directly extend jquery.fn?

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139

3 Answers3

18

From the jQuery UI Developer Guide:

Once the element is created, all other calls to the widget name where the first parameter is not a string will call the _init() method; if options are passed, the .option() method will be called before the _init() method

Mottie
  • 84,355
  • 30
  • 126
  • 241
4

The downside to extending widget (as opposed to $.fn) is that you create a dependency on jquery-ui which defines the widget "class". That dependency could be expensive for users of your plugin that don't also use jquery-ui.

As far as _create vs _init goes, I'm pretty sure that _init came first and then in a recent revision they introduced and favor _create. I might be wrong about this, but I believe that _init is still supported. If it is then there shouldn't be any differences between the two.

Ken Browning
  • 28,693
  • 6
  • 56
  • 68
  • 1
    Good point on widget. Do you have any replacement of widget if I don't want to introduce additional dependency to jqueryui? – Gelin Luo Oct 23 '10 at 12:23
  • 3
    You could just rip out the widget code and include it in your .js file. There was talk about moving widget into jquery itself instead of jqueryui but I don't know if that's been slated for a specific release or not yet. – Ken Browning Oct 23 '10 at 20:47
  • 5
    There is a different between the _init and _create. _create is called only once. _init will be called multiple time as mentionned from @fudgey – jsgoupil Jul 07 '11 at 21:36
3

A widget has three phases:

+-------+----------------+---------------+--------------------------------------------------------------------------------------------+
| Phase | Name           | jQuery Method | Description                                         |  
+-------+----------------+---------------+--------------------------------------------------------------------------------------------+
|     1 | Creation       | _create       | First time the widget is applied to an element, it is called.                              |  
|     2 | Initialization | _int          | The _init method is called after _create when the widget is first applied to its elements. |  
|     3 | Destruction    | destroy       | The widget's destroy method is used to detach a widget from an element.                    |  
+-------+----------------+---------------+--------------------------------------------------------------------------------------------+

NOTE: The method names starting with an underscore are meant to be private by convention.


So there is a difference between _create and _init. One is used for creation and the other is used for initialization. Every time you call the widget with no arguments or with options, it will indirectly call _init method. Therefore, this can be used to reset (re-initialize) a widget or pass it different options.

More details about each phase here.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64