Here is my own typeahead ember component:
Component
App.XTypeaheadComponent = Ember.Component.extend({
suggestionEngine: null,
data: null,
name: null,
selection: null,
init: function () {
var self = this;
this._super();
this.suggestionEngine = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace(self.name),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: this.data
});
this.suggestionEngine.initialize();
},
didInsertElement: function () {
this.$('#typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'name',
displayKey: this.name,
source: this.suggestionEngine.ttAdapter()
});
this.$().on('typeahead:selected', function (obj, dat, name) {
this.set('selection', dat);
}.bind(this));
},
willDestroyElement: function () {
this.$('#typeahead').typeahead('destroy');
}
});
Components Template
<script type="text/x-handlebars" data-template-name="components/x-typeahead">
{{input type='text' id='typeahead'}}
</script>
In Action:
http://emberjs.jsbin.com/vetaro/3/edit
No styling included
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…