Skip to content

Instantly share code, notes, and snippets.

@EdwardIrby
Created June 6, 2014 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdwardIrby/9145762d0a8fca513c1d to your computer and use it in GitHub Desktop.
Save EdwardIrby/9145762d0a8fca513c1d to your computer and use it in GitHub Desktop.
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