typeahead.js icon indicating copy to clipboard operation
typeahead.js copied to clipboard

typeahead.js stop working after some requests

Open sergiogregorutti opened this issue 12 years ago • 0 comments

Locally it works perfect. But, on my server, it works but after some requests this stop working.

Im using the "remote" option. I can see the petitions to the server and in a particular moment (after 25/40 petitions aprox) the PHP that returns the JSON it returns "null" and then it stop working.

I think that is a server configuration, but Im not sure.

This is the JS code:

$(document).ready(function() {
  var charMap = {
    "à": "a", "â": "a", "á": "a", "ä": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
    "ï": "i", "î": "i", "í": "i", "ô": "o", "ö": "o", "o": "o", "û": "u", "ù": "u", "ü": "u", "ú": "u"
  };

  var normalize = function(str) {
    $.each(charMap, function(chars, normalized) {
      var regex = new RegExp('[' + chars + ']', 'gi');
      str = str.replace(regex, normalized);
    });

    return str;
  }

  var queryTokenizer = function(q) {
    var normalized = normalize(q);
    return Bloodhound.tokenizers.whitespace(normalized);
  };

  var categorias = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('nombre'),
    queryTokenizer: queryTokenizer,
    name: 'categorias',
    remote: {url:'interfaces/buscar_categorias.php?frase=%QUERY',rateLimitWait:50},
  });

  var avisos = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('titulo'),
  queryTokenizer: queryTokenizer,
    name: 'avisos',
    remote: {url:'interfaces/buscar_avisos.php?frase=%QUERY',rateLimitWait:50},
  });

  categorias.initialize();
  avisos.initialize();

  $('#typeahead').typeahead({
    minLength: 1,
    highlight: true,
    hint: false
  }, {
    name: 'categorias',
    displayKey: 'busqueda',
    source: categorias.ttAdapter(),
    templates: {
      header: '<h3><i class="fa fa-folder-open-o antagon-color-main"></i> Categorías</h3>',
      suggestion: Handlebars.compile('<p>{{nombre}}</p>')
    }
  }, {
    name: 'avisos',
    displayKey: 'titulo',
    source: avisos.ttAdapter(),
    templates: {
      header: '<h3><i class="fa fa-list antagon-color-main"></i> Avisos</h3>',
      suggestion: Handlebars.compile('<p>{{titulo}}</p>')
    }
  }).on('typeahead:selected',function(aux,seleccionado){
    if (seleccionado.tipo == 'categoria') {
      location.href = 'resultados.php?idNivel'+seleccionado.nivel+'='+seleccionado.id;
    }else {
      location.href = 'aviso.php?id='+seleccionado.id;
    }
    return false;
  }).on('typeahead:cursorchanged',function(){
    return false;
  });
});

sergiogregorutti avatar Oct 20 '14 14:10 sergiogregorutti