小编Jes*_*sik的帖子

使用带有typeahead.js的预取过滤器(使用基本选项)

我正在使用typeahead.js.我正在使用预取选项,需要解析返回的数据.这不是一个错误; 它没有做太多的事情.我查找了示例,但没有一个使用Prefetch Filter选项.

链接到预取文档

我的代码(不起作用但不会引发错误):

$('#autocomplete').typeahead('destroy').typeahead( {                                
    name: 'organizations',
    prefetch: {
        url: 'jsoncall',
        filter: function() {
                        // Blatant hardcoded return value
                        return ['test-a','test-b','test'c];
                      }
        }
    }
);
Run Code Online (Sandbox Code Playgroud)

我的HTML:

<input id="autocomplete" class="large-12" autocomplete="off" type="text" placeholder="Enter school name">
Run Code Online (Sandbox Code Playgroud)

我的困惑:

0________0
Run Code Online (Sandbox Code Playgroud)

javascript jquery autocomplete typeahead.js

4
推荐指数
1
解决办法
9024
查看次数

如何使用没有根节点的JSON呈现Mustache模板

我正在构建一个小页面,使用jquery的getJSON调用API,然后使用胡子模板(至少是计划)呈现数据.但是我的JSON没有节点根,并且无法使用正确的胡子机制循环访问JSON响应数据.

//start
file : jsonresponse.jsonp

?([{"CountryIsoAlpha3Code":"AFG","CountryName":"Afghanistan"},
{"CountryIsoAlpha3Code":"ALA","CountryName":"Åland Islands"},
{"CountryIsoAlpha3Code":"ALB","CountryName":"Albania"}])
//end

//start
file: tmpl.mustache

<option value="{{CountryIsoAlpha3Code}}">{{CountryName}}</option>
//end

//start
file: render.js

$.getJSON(jsonresponse.jsonp, function(data) { 
   var tpl = tmpl.mustache,
   i=data.length,
   html = '';
     while(i--){
       html = html + mustache.render(tpl, data[i]);
     }

//end
Run Code Online (Sandbox Code Playgroud)

我意识到这是完全错误的(仅在模板使用中,假设我实际上正在传递数据和模板)但它确实有效.但如果我想做以下怎么办...我该怎么做!?:

//start
//file : daydreamer.mustache

<h1>This title is only awesome when it's rendered once</h1>
{{#}}  //no root node attempt at recursive templating rendering
     <option value="{{CountryIsoAlpha3Code}}">{{CountryName}}</option>
{{/}}

//end  
Run Code Online (Sandbox Code Playgroud)

如果不清楚请告诉我,我知道一个可怜的问题是眼睛疼痛.我会尽快编辑.谢谢.

jquery json jsonp mustache

2
推荐指数
1
解决办法
1214
查看次数

标签 统计

jquery ×2

autocomplete ×1

javascript ×1

json ×1

jsonp ×1

mustache ×1

typeahead.js ×1