I have an autocomplete function with callback to return data. This autocomplete function is used by many different instances.
$.fn.acItem = function(callback){
var self = this;
$(self).autocomplete({
(...)
select: function(e, ui){
// Eval for calling anonymous function (right?)
window[callback](ui.item);
}
});
};
I'm also using namespaces
var Agency = {
init: function() {
var self = this;
self.registerAgency.addItem();
},
registerAgency: {
(...)
addItem: function(item){
if(!item){
(...)
// Initiate autocomplete on input
$('.search-item').acItem('Agency.registerAgency.addItem');
} else {
// Do something with Item
}
}
}
Agency.init();
Using window[callback](ui.item)
works if I'm not using namespace. But with namespace, it gets a bit more complicated as described by Jason Bunting.
Since different namespaces is using acItem()
, it needs to know what function to call in the callback. But using Eval for calling anonymous function is strongly discouraged by many.
So what is a good way for doing callback on anonymous functions?