如何使用Bloodhound新的Typeahead进行错误处理?(How is error handling done with the new Typeahead with Bloodhound?)

编程入门 行业动态 更新时间:2024-10-11 19:17:31
如何使用Bloodhound新的Typeahead进行错误处理?(How is error handling done with the new Typeahead with Bloodhound?)

我有一个问题,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") } });

I have an issue in which Typeahead simply stops working when the user federated session expires. I would like to be able to perform an action when the "remote" call for Typeahead fails. How is this handled with Typeahead in particular? Is there some sort of "error" callback like you would find in a typical ajax call? Here is the code that I currently have:

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") } });

最满意答案

Typeahead的 Bloodhound建议引擎缺乏在远程数据源出现问题时通知用户的设施。

而不是使用Bloodhound来获得建议,而是使用Typeahead的源代码选项(请参见此处 )。 通过在此处指定来源,您可以处理错误并向用户显示合适的消息。

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

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); }); }

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

asyncResults(results);

Typeahead's Bloodhound suggestion engine is lacking in facilities to inform the user when there is a problem with a remote source.

Instead of using Bloodhound to get the suggestions you could instead use Typeahead's source source option (see here). By specifying your source here you can then handle errors and display a suitable message to the user.

I've created an example here:

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

The key part of the answer is source option code shown below:

$('.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); }); }

The source option is making use of jQuery's get method to retrieve the data. Any errors which occur are handled by the deferred object's fail method. In that method you can appropriately handle any errors and display a suitable message to the user. As the source function is specified with three parameters, this causes Typeahead to default this call as asynchronous, hence the call to:

asyncResults(results);

更多推荐

本文发布于:2023-08-04 19:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   错误   Bloodhound   Typeahead   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!