Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <template name="autocomplete">
- <input id="{{name}}" class="autocomplete-input {{className}}"
- placeholder="{{placeholder}}" autocomplete="off"/>
- <ul class="suggestions"></ul>
- </template>
- (function() {
- var inputEl, suggestionsEl, timeout, displayedSuggestions, selectedSuggestion;
- var search = function(inputString) {
- if (timeout) {
- clearTimeout(timeout);
- }
- timeout = setTimeout(function() {
- HTTP.get("https://photon.komoot.de/api/?q=" + inputString + "&lang=" + globalConfig.language, {},
- function(error, response) {
- if (error) {
- } else {
- displayedSuggestions = response.data.features;
- display(displayedSuggestions);
- }
- });
- }, 300);
- };
- var suggestionString = function (suggestion) {
- return suggestion.properties.name + ' (' + suggestion.properties.country + ')';
- };
- var selectSuggestion = function(elt) {
- inputEl.value = elt.innerHTML;
- selectedSuggestion = displayedSuggestions[elt['data-index']];
- suggestionsEl.style.display = 'none';
- };
- var display = function(data) {
- suggestionsEl.innerHTML = '';
- suggestionsEl.style.display = 'block';
- for (var i = 0; i < data.length; i++) {
- var suggestion = data[i];
- var li = document.createElement('li');
- li.className = "suggestion";
- li.onclick = function() {selectSuggestion(this);};
- li['data-index'] = i;
- li.innerHTML = suggestionString(suggestion);
- suggestionsEl.appendChild(li);
- }
- };
- Template.autocomplete.onRendered( function(){
- inputEl = this.find('.autocomplete-input');
- suggestionsEl = this.find('.suggestions');
- });
- Template.autocomplete.events({
- "keyup .autocomplete-input": function (event) {
- search(event.target.value);
- }
- });
- })();
- function(error, response) {
- if (error) {
- } else {
- displayedSuggestions = response.data.features;
- display(displayedSuggestions);
- }
- }
- // to
- function(error, response){
- if(error) return; // throw new Error('nothing') or console.warn('')
- displayedSuggestions = response.data.features;
- display(displayedSuggestions);
- }
- function debounce(fn, delay){
- var timer = null;
- return function(){
- if(timer) clearTimeout(timer);
- setTimeout(fn, delay || 300);
- }
- }
- var search = debounce(function(){
- // Your AJAX code
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement