Feedback Request - Autocomplete extension
I recently had a need for an autocomplete implemenation and I ended up making a custom extension of autocomplete that works around a couple of pieces of functionality causing me grief and adds in additional flexibility.
I didn't want to modify the actualy autocomplete plugin, so this is a more defensive approach. I am interested in feedback on the overall approach of this implementation. Am I breaking any "best practices"? Is there a better way to accomplish any given piece of this?
An example is attached as a zipped html file which includes the custom plugin as inline javascript. It is heavily commented, so hopefully it will be clear what is going on. Here is a brief summary of the goals of the plugin:
- Implement caching for remote data in a reusable manor.
- Adds support for "renderItem" option which allows for the override of the built-in implementation of _renderItem().
- Addresses race-conditions (search A starts; before response user changes search value and search B starts; Search B results are returned; Search A results are returned; Search A results are displayed when Search B results should be.)
- Works around issue where autocomplete results may not show up after backspacing and typing same characters again (http://forum.jquery.com/topic/autocomplete-options-not-redisplaying)
- Prevents searching from occuring when HOME or END is used inside the input control. (I personally don't think it makes sense for these keys to trigger a search).
Known issues: Javascript errors can occur in IE due to issue in jquery core which fires success handlers when abort() is called on an XHR instance. (
http://dev.jquery.com/ticket/6060) I have a patch in place in my version of the jquery core to prevent this problem.
Note, I did not have a remote url I could expose for this demo so I had to hack in some code to successfully use a geonames.org endpoint without a custom callback for the source option. These hacks are commented as such and should not be considered as part of the 'normal' algorithm.