Bind typehead to contact too

This commit is contained in:
Martin Lensment 2014-09-09 13:36:28 +03:00
parent 13349a6bc9
commit 1c165cd1e4
7 changed files with 55 additions and 30 deletions

View file

@ -0,0 +1,29 @@
class @Autocomplete
@bindTypeahead: (obj) ->
$(obj.selector).typeahead(
highlight: true,
hint: false
,
displayKey: "display_key"
source: Autocomplete.constructSourceAdapter(obj.remote)
).on('typeahead:selected', (e, item) ->
$(obj.hiddenSelector).val item.id
ok = $(obj.hiddenSelector).parent('div.has-feedback').find('.js-typeahead-ok')
remove = $(obj.hiddenSelector).parents('div.has-feedback').find('.js-typeahead-remove')
ok.removeClass('hidden')
remove.addClass('hidden')
)
@constructSourceAdapter: (remote) ->
source = new Bloodhound(
datumTokenizer: (d) ->
Bloodhound.tokenizers.whitespace d.display_key
queryTokenizer: Bloodhound.tokenizers.whitespace
remote: "#{remote}?q=%QUERY"
)
source.initialize()
source.ttAdapter()