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 ?