typeahead预取不起作用

时间:2013-10-29 22:16:05

标签: json typeahead prefetch

我已经找到了答案但仍无法找到答案。我想我做错了但我无法弄清楚它是什么。这是我的先行代码:

$('.zoek').typeahead({
name: 'items01',
prefetch: {
url: '/models/_global.json', 
ttl: 1,
},

template: '<div id="breed">
       <span>{{#lock}}<img src="/assets/img/lock-icon-small.png">{{/lock}}{{^lock}}<img    src="/assets/img/empty-icon-small.png">{{/lock}}</span> 
       <span class="searchresult"><a href="article">{{value}}</a></span>
       <span class="searchresult-type"> - {{type}} </span>
       <span class="red">{{#popular}} - popular {{/popular}}</span>
       <span class="red">{{#new}} - new {{/new}}</span>

       </div>',
engine: Hogan

});

这是我的_global.json文件:

{

"items": [

 {"value":"making claims for ebook subscriptions",
  "icon":"download-icon-small.png", 
  "type":"brochure",
  "date":"12-10-2011",
  "lock": false,
   "popular": false,
   "new": true,
   "tag": ["ebook","claim"]},

 {"value":"Claim discovery & Online Management Suite - English",
  "icon":"download-icon-small.png",  
  "type":"case",
  "date":"28-02-2010",
  "lock": true,
  "popular": false,
  "new": false,
  "tag": ["claim","online"]},

 {"value":"Get access to all online ebook publishers ",
  "icon":"download-icon-small.png", 
  "type":"factsheet",
  "date":"17-08-2012",
  "lock": false,
  "popular": true,
  "new": true,
  "tag": ["online","ebook"]},

 {"value":"Ebook catalogue in swetswise - howto",
  "icon":"show-icon-small.png", 
  "type":"hint",
  "date":"08-01-2009",
  "lock": true,
  "popular": false,
  "new": false,
  "tag": ["ebook","swetswise"]},

 {"value":"Swetswise software improved: you can claim now!",
  "icon":"download-icon-small.png", 
  "type":"press",
  "date":"20-08-2002",
  "lock": false,
  "popular": true,
  "new": false,
  "tag": ["swetswise","claim"]}
]

}

Json文件不能有其他格式,因为其他一些代码依赖于这种格式。预取不起作用,我想我必须使用过滤器。尝试了我能找到的各种过滤器示例,但似乎没有任何效果。我做错了什么。

P.S。 (当我在json文件中省略顶部花括号和键“items”时,它们都正常工作,因此js。库和Hogan没有问题)

希望有人可以帮助我。

问候。 M.dekker

1 个答案:

答案 0 :(得分:2)

经过良好的睡眠和一些思考,找到了我的问题的答案。

在预取块中添加以下代码解决了我的问题:

  filter: function(data){
           // filter the returned data
           return data.items;
    }
相关问题