I am using Knockout JS in combination with this combobox extension for jQuery: http://source.dellsala.com/jquery-combobox/demo/
I have a custom binding event when the view model initializes:
ko.bindingHandlers.dataOptions = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var id = ko.utils.unwrapObservable(valueAccessor());
var vm = viewModel;
var data = vm.Options();
var objArray = new Array();
$.each(data, function (i) {
var obj = new gv.objectType(data[i].attributeId(), data[i].Name());
objArray.push(obj);
});
var sel = '#' + vm.Id();
$(sel).combobox(objArray);
//$(element) didn't work as a selector.
//Only the actual string selector works: $('#someid')
}
};
For some crazy reason, I am unable to use $(element) or a dynamic selector (based on a variable) in order to generate a combobox:
$(element).combobox(objArray);
However, if I directly input the string selector:
$('#myId').combobox(objArray);
It works fine. Also any standard jQuery methods work fine with a dynamic selector:
$(element).val('hello world');
So I think this is an issue with the combobox plugin. The thing is, I REALLY want to use this specific combobox plugin after reviewing several.
Any ideas? Thanks!
Solved it: You can’t trigger certain plugin’s (outside of Knockout) dynamically until you apply bindings to the knockout elements. It must be that KO doesn’t register the elements to the DOM until the bindings are applied.