typeahead.js不显示JSON对象数组的结果

时间:2016-11-17 14:53:20

标签: javascript json typeahead.js bloodhound

我正在使用带有Bloodhound的typeahead.js来远程搜索我们的产品,但即使我收到了有效的json响应,empty模板也会一直显示。

products = new Bloodhound(
  datumTokenizer: Bloodhound.tokenizers.whitespace
  queryTokenizer: Bloodhound.tokenizers.whitespace
  remote:
    url: '/search.json?search[q]=%QUERY'
    wildcard: '%QUERY'
)

$('#search_q').typeahead null,
  source: products.ttAdapter()
  templates:
    empty: '
      <div class="empty-message">
        Unable to find any results that match the current query
      </div>
    '
    suggestion: Handlebars.compile '
      <div>
        <strong>{{name}}</strong> – {{url}}
      </div>
    '

服务器在输入时返回此JSON响应:

[
{"name":"Shankh Terracotta Tea Light Holder ","type":"Products","url":"/goods/6476/shankh-terracotta-tea-light-holder"},
{"name":"Tess Wallet ","type":"Products","url":"/goods/3587/tess-wallet"},
{"name":"Shankh Shaped Terracotta Handpainted Tea Light Holder","type":"Products","url":"/goods/19483/shankh-shaped-terracotta-handpainted-tea-light-holder"},
{"name":"Shankh Shaped Terracotta Handpainted Tea Light Holder","type":"Products","url":"/goods/19487/shankh-shaped-terracotta-handpainted-tea-light-holder"},
{"name":"Traditional Golden Tea Pot","type":"Products","url":"/goods/1341/traditional-golden-tea-pot"}
]

0 个答案:

没有答案