【问题标题】:What is a proper way to handle exceptions thrown by typeahead/Bloodhound code?处理 typeahead/Bloodhound 代码抛出的异常的正确方法是什么?
【发布时间】:2014-06-26 15:04:11
【问题描述】:

如果服务器中的数据不可用或发生服务器错误或服务器数据为空,则 javascript 将抛出异常 obj is null。如何捕捉这个异常?

在 stackoverflow 中搜索没有给出任何结果。

代码如下:

var test1 = new Bloodhound({
    datumTokenizer: function (d) {
        return Bloodhound.tokenizers.whitespace(d.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: "http://URL?query=",

        replace: function(url, query) {
            return url + "" + query;
        }
    }
});

test1.initialize();

$('#idOfAutoCompleteTextBox').typeahead({
    hint: true,
    highlight: true,
    minLength: 1
},
{
    name: 'test1',
    displayKey: 'value',
    source: test1.ttAdapter()
});

【问题讨论】:

    标签: typeahead.js bloodhound


    【解决方案1】:

    我不确定这是否可以解决您的问题,但目前我在处理空结果时遇到相同的 javascript 异常(提前输入 0.11.1)。

    在远程 URL 文件中,当数组为空时,回显非 JSON 格式的内容。

    JavaScript

      var termekek = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('megnev'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
          url: _BASE_ +  'components/com_rexwebshop/include/keres.php?q=%QUERY',
          wildcard: '%QUERY'
        }
      });
    
      jQuery('.typeahead').typeahead(null, {
        name: 'rs3ws-termekek',
        display: 'megnev',
        source: termekek,
          templates: {
            empty: [
              '<div class="empty-message">',
                '<? echo JTEXT::_('Nincs találat') ?>',
              '</div>'
            ].join('\n'),
            suggestion: Handlebars.compile('<div><strong>{{megnev}}</strong> – {{bruttoar}}</div>')
          }
      });
    

    PHP

      // [collecting data]
    
      if ( count($rows) == 0 ) 
        echo "_NOT_FOUND_";
      else
        echo json_encode($rows, JSON_UNESCAPED_SLASHES);
    

    【讨论】:

    • 您能否详细说明这将如何帮助解决原始发帖人问题中的问题?我不太关注你。
    • remote php's source is not present in the question,所以我无法给出确切的解决方案
    猜你喜欢
    • 1970-01-01
    • 2013-08-31
    • 2012-11-18
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多