【问题标题】:How is error handling done with the new Typeahead with Bloodhound?新的 Typeahead with Bloodhound 如何处理错误?
【发布时间】:2016-04-04 13:42:29
【问题描述】:

我遇到了一个问题,即当用户联合会话到期时,Typeahead 会停止工作。我希望能够在 Typeahead 的“远程”调用失败时执行操作。特别是 Typeahead 是如何处理的?是否有某种“错误”回调,就像您在典型的 ajax 调用中发现的那样?这是我目前拥有的代码:

var hints = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: "/ProjectAssociation/CountryLookup?query=%QUERY",
        wildcard: "%QUERY"
    }
});
$("#assocStoragesSelection").typeahead(null, {
    name: "nations",
    limit: 90,
    valueKey: "ShortCode",
    displayKey: "Name",
    source: hints,
    templates: {
        empty: [
            "<div class='noitems'>",
            "No Items Found",
            "</div>"
        ].join("\n")
    }
});

【问题讨论】:

  • 在这种情况下,您需要添加从服务器返回的数据签入;
  • 我的回答有帮助吗?

标签: javascript jquery typeahead.js typeahead bloodhound


【解决方案1】:

处理错误的“正确”方法是使用prepare 函数向AJAX 调用添加错误处理程序。如果您使用wildcard 选项,请注意prepare 会覆盖它。

例如,你可以这样转:

new Bloodhound({
    remote: {
        url: REMOTE_URL,
        wildcard: '%s'
    }
});

进入这个:

new Bloodhound({
    remote: {
        url: REMOTE_URL,
        prepare: function(query, settings) {
            return $.extend(settings, {
                url: settings.url.replace('%s', encodeURIComponent(query)),
                error: function(jqxhr, textStatus, errorThrown) {
                    // show error message
                },
                success: function(data, textStatus, jqxhr) {
                    // hide error message
                }
            });
        }
    }
});

prepare返回的对象作为jQuery.ajax()的设置对象,可以参考its documentation

【讨论】:

    【解决方案2】:

    Typeahead'sBloodhound 建议引擎缺乏在远程资源出现问题时通知用户的功能。

    您可以使用 Typeahead 的 source source 选项代替使用 Bloodhound 获取建议(请参阅 here)。通过在此处指定您的来源,您可以处理错误并向用户显示合适的消息。

    我在这里创建了一个示例:

    http://jsfiddle.net/Fresh/oqL0g7jh/

    答案的关键部分是如下所示的源选项代码:

    $('.typeahead').typeahead(null, {
      name: 'movies',
      display: 'value',
      source: function(query, syncResults, asyncResults) {
        $.get(_url + query, function(movies) {
    
          var results = $.map(movies.results, function(movie) {
            return {
              value: movie.original_title
            }
          });
    
          asyncResults(results);
        }).fail(function() {
          $('#error').text('Error occurred during request!');
          setTimeout("$('#error').text('');", 4000);
        });
    }
    

    source 选项是利用 jQuery 的 get 方法来检索数据。发生的任何错误都由延迟对象的失败方法处理。在该方法中,您可以适当地处理任何错误并向用户显示合适的消息。由于源函数是用三个参数指定的,这会导致 Typeahead 将此调用默认为异步,因此调用:

    asyncResults(results);
    

    【讨论】:

      【解决方案3】:

      试试这个代码

      var hints = new Bloodhound({
          datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          remote: {
              url: "/ProjectAssociation/CountryLookup?query=%QUERY",
              wildcard: "%QUERY",
              ajax: {
               error: function(jqXHR) {
                //do some thing
               }
          }
          }
      });
      

      【讨论】:

      • @BenSmith 的回答很优雅,但如果这个有效,我相信它是最简洁的。
      • @AngelJosephPiscola 此答案不适用于最新版本的 Bloodhound。 'remote' 选项没有 'ajax' 选项。见here
      • 这在 10.5 中效果很好......经过几个小时的搜索和一堆其他只处理空结果集的答案,而不是处理远程 url 返回的实际错误的答案(401s 等)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      相关资源
      最近更新 更多