Created
May 3, 2011 10:54
-
-
Save marksteve/953154 to your computer and use it in GitHub Desktop.
Backbone View for jQuery UI Autocomplete inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Autocomplete = Backbone.View.extend({ | |
render: function() { | |
var choices = this.options.choices, | |
selected = this.options.selected, | |
iterator = this.options.iterator, | |
label = this.options.label, | |
allowDupes = this.options.allowDupes, | |
$el = $(this.el); | |
$el.autocomplete({ | |
source: function(request, response) { | |
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i'); | |
response(choices.filter(function(model) { | |
return iterator(model, matcher); | |
})); | |
}, | |
focus: function(event, ui) { | |
$el.val(label(ui.item)); | |
return false; | |
}, | |
select: function(event, ui) { | |
selected.add(ui.item); | |
if (!allowDupes) { | |
choices.remove(ui.item); | |
} | |
$el.val(''); | |
return false; | |
} | |
}).data('autocomplete')._renderItem = function(ul, item) { | |
return $('<li/>') | |
.data('item.autocomplete', item) | |
.append($('<a/>').text(label(item))) | |
.appendTo(ul); | |
}; | |
return this; | |
} | |
}); |
Trimmed of some redundant code (some stuff that Backbone already does w/c aren't really documented) and wrote an example (http://tinkerbin.com/gTmLJuRE).
Links are expired. I'd like to see your article.
Working URL is here: http://blog.marksteve.com/post/5186752058/autocomplete-in-backbone-js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
link to the article since this gist comes up as a higher result in google