ko.bindingHandlers.autoComplete = {
// Only using init event because the Jquery.UI.AutoComplete widget will take care of the update callbacks
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var settings = valueAccessor();
var selectedOption = settings.selected;
var options = settings.options;
var updateElementValueWithLabel = function (event, ui) {
// Stop the default behavior
event.preventDefault();
// Update the value of the html element with the label of the activated option (ui.item)
//$(element).val(ui.item.label); // This does not udate our 'tagname' observable
bindingContext.$root.tagname(ui.item.label);
// Update our SelectedOption observable
if(typeof ui.item !== "undefined") {
// ui.item - label|value|...
selectedOption(ui.item);
}
};
$(element).autocomplete({
source: options,
select: function (event, ui) {
updateElementValueWithLabel(event, ui);
//alert("select: event,ui.item.label="+event+"/"+ui.item.label);
},
focus: function (event, ui) {
updateElementValueWithLabel(event, ui);
},
change: function (event, ui) {
updateElementValueWithLabel(event, ui);
}
});
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
//what to do here?
// this is never called after initialization... hmm
// Update the observable with the label of the activated option (ui.item)
//$(element).val(ui.item.label);
//bindingContext.$root.tagname(ui.item.label);
}
};
//----------------------------------------- bindingHandlers above….
/* global ko */
String.prototype.trim = function () {
return...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.