Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI options versus methods

What is the semantic difference between jQuery UI widget options and methods?

Initialize slider widget with options object defining the value option.

$('#mySlider').slider({ value: 10});

Get value option.

var optValue = $('#mySlider').slider('option', 'value');

Get value.

var methodValue = $('#mySlider').slider('value');

Is it possible that optValue and methodValue can be different?

like image 835
gravidThoughts Avatar asked Jan 24 '26 17:01

gravidThoughts


1 Answers

The short answer is, there is no semantic difference. option merely calls the underlying get/set.

On widget methods: When you call $(element).widget("someMethod", parameters) what happens is (for the purposes of the question) a call to widgetPrototype.someMethod(parameters).

widget.("option", property) is a special case of widget method. As a getter, it goes straight to the underlying options object, but as a setter calls the widget._setOptions() function. From the documentation:

option( options )

Returns: jQuery (plugin only)

Sets one or more options for the widget.

options Type: Object A map of option-value pairs to set.

Here's a breakdown of what happens:

$('#mySlider').slider({ value: 10});
// call the slider widget's initialiser with parameter {value:10}

var optValue = $('#mySlider').slider('option', 'value');
// call the slider widget's .option() method with parameter value

var methodValue = $('#mySlider').slider('value');
// calls the slider widget's .value() method.
//
// EDIT: Yes, this CAN be different from .option(value)
// It falls on the implementer to document their widget well
// enough that users know what's going on
//
// Having said that, I have yet to see a popular jquery widget
// where .someWidgetOption() is not just a shortcut to .option("someWidgetOption")

The documentation is remarkably good: http://api.jqueryui.com/jQuery.widget/

like image 115
blgt Avatar answered Jan 27 '26 05:01

blgt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!