concept _getCreateOptions() in category jQuery

This is an excerpt from Manning's book jQuery UI in Action.
The last widget method we’re going to discuss is one of the more useful ones; it’s the basis of how jQuery Mobile’s autoinitialization works. The method is _getCreateOptions(), and it gives you the ability to define options during widget initialization. When you provide a _getCreateOptions() method, you can define options that override the widget’s defaults. Consider the following code that creates a test widget:
![]()
Each time the test widget’s constructor function is called, the _getCreateOptions() method is invoked
. Here the _getCreateOptions() method returns a hardcoded foo option, which overrides the widget’s default value for this option ("bar") with "bang".
Because of this override, when you create an instance with no options, its foo option is set to "bang"
. Despite the override, user-supplied options still override the values given in _getCreateOptions(); therefore, your second instance maintains the "biz" option that was passed to its constructor
.
Now that you have an idea of how _getCreateOptions() works, let’s see how this is useful, starting with a few ways that jQuery UI itself uses these methods. The jQuery UI selectmenu widget uses the following method:
_getCreateOptions: function() { return { disabled: this.element.prop( "disabled" ) }; }The context of _getCreateOptions() (this) is set to the widget instance, which gives access to the element the widget is being created on. The selectmenu widget uses that reference to default its disabled option to whether the <select> it’s initializing is disabled. Deriving widget options from element attributes like this is the most common use of _getCreateOptions(). The spinner widget does something similar.
. Here the _getCreateOptions() method returns a hardcoded foo option, which overrides the widget’s default value for this option ("bar") with "bang".
. Despite the override, user-supplied options still override the values given in _getCreateOptions(); therefore, your second instance maintains the "biz" option that was passed to its constructor
.