Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RactiveJS Twitter Typeahead Component
<input class="flex-input-field" decorator="typeahead">
twitterTypeahead = Ractive.extend({
template: require('./twitterTypeahead.html'),
setData: function(options) {
data = options.data;
},
beforeInit:function(options){
this.setData(options)
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push({ value: str });
}
});
cb(matches);
};
};
var typeaheadDecorator = function (node) {
me = typeaheadDecorator;
$(node).typeahead({
hint: me.hint,
highlight: me.highlight,
minLength: me.minLength
},
{
name: me.name,
displayKey: me.displayKey,
source: me.source
});
return {
teardown: function () {
$(node).typehead('destroy');
}
};
}
// Default parameters
typeaheadDecorator.hint = data.hint;
typeaheadDecorator.highlight = data.highlight;
typeaheadDecorator.minLength = data.minLength;
typeaheadDecorator.name = data.name;
typeaheadDecorator.displayKey = data.displayKey;
typeaheadDecorator.source = substringMatcher(data.source);
Ractive.decorators.typeahead = typeaheadDecorator
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.