rniemeyer / knockout-jqAutocomplete

knockout-jqAutocomplete is a Knockout.js plugin designed to work with jQuery UI's autocomplete widget.
MIT License
49 stars 20 forks source link

Overriding "base" configuration #8

Closed pkmccaffrey closed 9 years ago

pkmccaffrey commented 9 years ago

Is it possible to override some of the base configuration (select, change, etc.)? I want to force the user to select a value from the list, and not allow free-form text entry.

My basic idea is to pass in my change function to the global ko.bindingHandlers.jqAuto.options object as follows:

ko.bindingHandlers.jqAuto.options = {
    minLength: 3,
    change: function (event, ui) {
        if (ui.item == null || ui.item == undefined) {
            $(this).val("");
        }
    }
}

When the binding initializes, it extends my options over the config object (line 20):

//extend with global options
ko.utils.extend(config, self.options);
//override with options passed in binding
ko.utils.extend(config, options.options);

But it then redefines config.change before initializing the Autocomplete plugin, effectively throwing my change function away (line 53):

//user made a change without selecting a value from the list
config.change = function (event, ui) {
    if (!ui.item || !ui.item.actual) {
        options.value(event.target && event.target.value);
        if (ko.isWriteableObservable(options.dataValue)) {
            options.dataValue(null);
        }
    }
 };

Is there a way to achieve the functionality that I want without modifying the binding code? If not, it's not the end of the world, but I'd like to keep my binding up to date with what is in this repo.

Please let me know if anything is unclear.

rniemeyer commented 9 years ago

@svtguy88 - it is not possible currently, as the binding configures its own change and select handlers. It wouldn't be too much work to ensure that any passed in change and select handlers are also called within the bindings own versions of them. Do you have any interest in potentially creating a pull request with this functionality?

pkmccaffrey commented 9 years ago

Wow - thanks for an unbelievably fast reply. Also, hello from a fellow Wisconsinite.

That being said, no problem. I'll make the changes, get everything working in my project and then submit a pull request. Are you thinking the code should use the passed in handlers to totally replace the binding's implementations, or be used in addition to them?

Realistically, I'll probably have it done shortly after the holidays. Maybe sooner if I find some free time in the coming days.

rniemeyer commented 9 years ago

@svtguy88 - I had a moment to work on this, so I made these changes in the latest version. Let me know if you run into any issues. Thanks!

pkmccaffrey commented 9 years ago

@rniemeyer - I'll try it out today, and let you know if I run into any issues. It looks like it should do exactly what I was looking for. Thanks again!