Now that we’re using Backbone JS as our Javascript framework, we find that having widgets that talk Backbone simplifies our code. Last week I took the time to write an autocomplete widget which uses a Backbone collection of models to feed it. I wrote it as a jQuery plugin and the self-documented code is here. You can give it a spin; here is a demo.
Type in a State or Province
To use it, simply call jQuery.autocomplete on an INPUT element. When the INPUT takes focus and the user starts typing, the widget runs over items in the collection and shows matches highlighting in bold the found character sequence. The drop-down list is simply a UL placed at absolute position. You can use the arrow keys to cycle through matches and press TAB or ENTER to select it. Of course, you can click with the mouse to select a match as well.
Here is the code for the above example. The Javscript demo file is here and the CSS here.
$(document).ready(function() {
var collection = new Backbone.Collection([
{id:"AB", name:"Alberta"},
{id:"BC", name:"British Columbia"},
{id:"MB", name:"Manitoba"},
// ...
{id:"AP", name:"Armed Forces Pacific"}
]);
$('#states_provinces').autocomplete({
collection: collection,
attr: 'name',
noCase: true,
ul_class: 'autocomplete shadow',
ul_css: {'z-index':1234}
});
});
If you’re a Backbone JS developer, feel free to use it and enhance it. One thing it currently lacks is the ability for users to scroll the content of the drop-down list (it is hidden upon INPUT blur – so clicking on the scrollbar hides it). But for short lists which don’t need to be scrolled, it works great.
Martin
Hey, looks like on line 214 you need to do
if (options.li_class) li_em.addClass(options.li_class);
instead of just addClass(li_class);
other than that, works great, thanks
Hi,
Thanks for your work. Do you have a github repository about this ?
Regards,
Hello Xavier, yes we do!
You can find it here: https://github.com/martindrapeau/Backbone-Widgets/
Let me know if this helps!
GET http://www.planbox.com/html/widgets/jquery.backbone.widgets.css 404 (Not Found) jquery-autocomplete-plugin-for-backbone-js.html:19
GET http://www.planbox.com/html/widgets/3rd-party/jquery.min.js 404 (Not Found) jquery-autocomplete-plugin-for-backbone-js.html:19
GET http://www.planbox.com/html/widgets/3rd-party/underscore.js 404 (Not Found) jquery-autocomplete-plugin-for-backbone-js.html:19
GET http://www.planbox.com/html/widgets/3rd-party/backbone.js 404 (Not Found) jquery-autocomplete-plugin-for-backbone-js.html:19
GET http://www.planbox.com/html/widgets/jquery.backbone.widgets.js 404 (Not Found) jquery-autocomplete-plugin-for-backbone-js.html:19
GET http://www.planbox.com/html/widgets/test.js 404 (Not Found)
Thank you for letting us know. They’ve been fixed.
Hi!
Couldn’t find any information on github – which license applies to the code? (MIT, Apache, public domain, gpl)
Thanks for sharing!
Jochen
Hi Jochen,
You can find some information here: http://www.planbox.com/html/widgets/jquery.backbone.widgets.js, under MIT.
However, please note that this library is no longer maintained. Hope this helps! If you need to know anything else, please let me know.