An autocomplete component for ember.js using typeahead.js
Include typeahead.js
, bloodhound.js
and ember-typeahead.js
<script src="typeahead.js"></script>
<script src="bloodhound.js"></script>
<script src="ember-typeahead.js"></script>
Add the typeahead tag in your handlebars template
{{type-ahead local=content name="colour" selection=myColour}}
This component can automatically search sources of data using a preconfigured Bloodhound.js adapter. To provide data to this adapter the following arguments can be set on the component. To use a custom search adapter specify the source parameter specified below.
local
- An array of ember objects used for the lookup. This can also be a promise that resolves to an array. Will be ignored ifsource
is set.prefetch
- Can be a URL to a JSON file containing an array of datums or, if more configurability is needed, a prefetch options hash.remote
- Can be a URL to fetch suggestions from when the data provided by local and prefetch is insufficient or, if more configurability is needed, a remote options hash.
source
- The backing data source for suggestions. Expected to be a function with the signature(query, cb)
. It is expected that the function will compute the suggestion set (i.e. an array of JavaScript objects) forquery
and then invokecb
with said set.cb
can be invoked synchronously or asynchronously. A Bloodhound suggestion engine can be used here, to learn how, see Bloodhound Integration. Required.
selection
- Binds the selected value. This changes on thetypeahead:selected
andtypeahead:autocompleted
events (see typeahead.js custom events)
highlight
– Iftrue
, when suggestions are rendered, pattern matches for the current query in text nodes will be wrapped in astrong
element withtt-highlight
class. Defaults tofalse
.hint
– Iffalse
, the typeahead will not show a hint. Defaults totrue
.minLength
– The minimum character length needed before suggestions start getting rendered. Defaults to1
.
-
name
– The name of the dataset. This will be appended tott-dataset-
to form the class name of the containing DOM element. Must only consist of underscores, dashes, letters (a-z
), and numbers. Defaults to a random number. -
displayKey
– For a given suggestion object, determines the string representation of it. This will be used when setting the value of the input control after a suggestion is selected. Can be either a key string or a function that transforms a suggestion object into a string. Defaults tovalue
. -
templates
– A hash of templates to be used when rendering the dataset. Note a precompiled template is a function that takes a JavaScript object as its first argument and returns a HTML string.-
empty
– Rendered when0
suggestions are available for the given query. Can be either a HTML string or a precompiled template. If it's a precompiled template, the passed in context will containquery
. -
footer
– Rendered at the bottom of the dataset. Can be either a HTML string or a precompiled template. If it's a precompiled template, the passed in context will containquery
andisEmpty
. -
header
– Rendered at the top of the dataset. Can be either a HTML string or a precompiled template. If it's a precompiled template, the passed in context will containquery
andisEmpty
. -
suggestion
– Used to render a single suggestion. If set, this has to be a precompiled template. The associated suggestion object will serve as the context. Defaults to the value ofdisplayKey
wrapped in ap
tag i.e.<p>{{value}}</p>
.
-
I didn't do any tests. For every line of untested code a puppy dies. 59 puppies died in the making of this component.