vickramravichandran / angular-auto-complete

AngularJS auto complete plugin
http://demo.vickram.me/angular-auto-complete
Other
28 stars 31 forks source link

fixes #21 - cannot read property autoHideDropdown of undefined #22

Closed andrei-gheorghiu closed 6 years ago

andrei-gheorghiu commented 6 years ago

fix for https://github.com/vickramravichandran/angular-auto-complete/issues/21

vickramravichandran commented 6 years ago

Why is it failing only there? The properties of that.options are read in many other places. For instance: getSelectedCssClass() reads that.options.selectedCssClass _query() reads that.options.pageSize & that.options.pagingEnabled

...and so on...

andrei-gheorghiu commented 6 years ago

I think the lib is placing a listener on the window object which checks the options to see if it should run the autoHide or not. But the instance has been destroyed. I'm not an expert on routing and how tabbed views work in angular and what happens there at DOM level, but the error is real.

If I must, I'll try to create a fiddle with the error. But I don't see any downside to this fix. Is there any?

vickramravichandran commented 6 years ago

If that.options might be undefined then I would like to apply the fix throughout the plugin. Here is what I have in mind...

Create a object hash of valid options from defaultOptions

var OPTIONS = {};
_.each(_.keys(defaultOptions), function(key) { 
    OPTIONS[key] = key;
});

/*
The result of the code above will be...
var OPTIONS = {
    selectedCssClass: 'selectedCssClass',
    autoHideDropdown: 'autoHideDropdown',
    ...
    ...
}
*/

Now I can create a helper method to get an option value instead of accessing it directly...

function _getOption(option, defaultValue) {
    if (OPTIONS.hasOwnProperty(option) && that.options && that.options.hasOwnProperty(option)) {
        return that.options[option];
    }

    return defaultValue;
}

Now I can replace all instances of "options.property" with

_getOption(OPTIONS.autoHideDropdown);
_getOption(OPTIONS.selectedCssClass);
vickramravichandran commented 6 years ago

I cherry picked your commit and merged to master. I have published version 1.7.3 to npm.