I am very new to jquery and recently tailored the jquery combobox as per my needs. However, I need to reuse this component in other places on my site. I essentially need to pass in some arguments and a callback function which should be called for handling certain events in jquery. I'm struggling with the syntax and trying to find jquery-way of doing things:
To give you a sample, here is the source
of combobox -> input -> autocomplete -> source
:
(Created a working jsfiddle for reference)
source: function( request, response ) {
// implements retrieving and filtering data from the select
var term = request.term;
var prefixLength = 2;
if (term.length >= prefixLength) {
var abbreviation = term.substring(0, prefixLength);
if(!(abbreviation.toLowerCase() in cache)){
cache[abbreviation.toLowerCase()] = 1;
$.ajax({
url: "/wah/destinationsJson.action",
dataType: "json",
data: {
term: abbreviation
},
type: "GET",
success: function(data) {
if(!data || !data.cities || !data.cities.length){
// response(["No matches for " + term]);
return;
}
updateOptions(select, data.cities);
response(filterOptionsForResponse(select, term));
return;
}
});
}
}
response(filterOptionsForResponse(select, term));
}
Here updateOptions(...)
, filterOptionsForResponse(select, term)
are simple javascript functions.
In order to reuse, I need to specify a callback to handle source
for every instance of combobox that I create.
Can someone point me in the right direction on how to do this ?
Under the hoods, every instance of every widget is stored on the element using jQuery. data() . To retrieve the instance object, call jQuery. data() using the widget's full name as the key.
We can extend jQuery widgets by using mixins. In java script mixin is a class whose methods are added to, or mixed in, with another class. In order to extend jQuery widget first we need to declare a mixin in requirejs-config.
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.
This will initialize each element in the jQuery object, in this case the element with an id of "elem". Because we called the . progressbar() method with no parameters, the widget is initialized with its default options. We can pass a set of options during initialization in order to override the default options.
The only useful widget factory documentation is in the wiki and that says:
Properties:
[...]
this.options
The options currently being used for the plugin configuration. On instantiation, any options provided by the user will automatically be merged with any default values
So if you needed to supply a callback function for source
, then you'd say this:
$("#searchbox").combobox({
source: function(request, response) { /* ... */ }
});
and then inside your plugin, look at this.options.source
:
$.widget("ui.combobox", {
options: {
// Default options go here...
},
_create: function() {
//...
input = $("<input>").appendTo(wrapper).val(value).addClass("ui-state-default").autocomplete({
source: this.options.source,
//...
You don't have to include the default options but it almost always makes sense to do so (even if you just use it for documentation purposes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With