Created
June 6, 2014 21:11
-
-
Save EdwardIrby/9145762d0a8fca513c1d to your computer and use it in GitHub Desktop.
RactiveJS Twitter Typeahead Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input class="flex-input-field" decorator="typeahead"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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