Skip to content

Instantly share code, notes, and snippets.

@finwo
Created November 15, 2016 14:55
Show Gist options
  • Select an option

  • Save finwo/6439801fee243496353f92d175aa7ff8 to your computer and use it in GitHub Desktop.

Select an option

Save finwo/6439801fee243496353f92d175aa7ff8 to your computer and use it in GitHub Desktop.
define('autocomplete', ['jquery'], function($) {
return function(selector) {
$(selector).each(function() {
var $element = $(this),
source = $element.data('autocomplete'),
cache = {};
$element.attr('autocomplete', 'off');
$element.typeahead({
source : function(query, done) {
if (cache[query]) {
return cache[query];
}
var uri = source.replace('%s', query);
$.get(uri)
.done(function(result) {
done(cache[query] = result.shift());
});
}
});
})
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment