如果没有返回命中,则为 Algolia 模板

Algolia template if no hits are returned

我已经基于 https://www.algolia.com/doc/search/auto-complete 实现了我自己的 Algolia PoC,现在我正在为一个特定的用例而苦苦挣扎:我如何处理没有 return 任何命中的搜索?

这是我的代码:

我已经能够识别和检测 when/where 没有命中 returned,但除了使用 console.log() 之外我什么也做不了。我试图获得一个自定义 return_msg 但我无法调用该函数。 我还尝试根据建议进行一些调整:function(suggestion) 但如果没有命中 returned,则永远不会调用此函数。 我也没有在 https://github.com/algolia/autocomplete.js

上找到关于此 "Templates" 部分的任何文档
$('#q').autocomplete({ hint: false }, [
    {
      source: function(q, cb) {
        index.search(q, 
          { hitsPerPage: 10 }, 
          function(error, content) {
                  if (error) {
                    cb([]);
                    return;
                  }

                  if (content.nbHits == 0)
                    { return_msg = '<h5> Sorry, no result </h5>';
                      // DO something here
                      console.log(return_msg);
                     // console.log return "Sorry, no result"
                     }

                  cb(content.hits, content);

                });
          },
      displayKey: 'game',
      templates: {
        suggestion: function(suggestion) {
          return_msg = '<h5> '+ suggestion.MY_ATTRIBUTE + '</h5>'
        return return_msg;
        }
      }
    }
  ]).on('autocomplete:selected', function(event, suggestion, dataset) {
     window.location = (suggestion.url);
  });

任何指点将不胜感激 =)

使用数据集的 templates 选项,您可以指定没有结果时要使用的模板:

source: autocomplete.sources.hits(indexObj, { hitsPerPage: 2 }),
templates: {
  suggestion: // ...
  header:  // ...
  footer:  // ...
  empty: function(options) {
    return '<div>My empty message</div>';
  }
}

完整文档here