我正在构建一个小页面,它使用 jquery 的 getJSON 调用 API,然后使用 mustache 模板呈现数据(至少这是计划)。但是,我的 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
我意识到这是完全错误的(仅在模板使用中,假设我实际上正确地传递了数据和模板)但它确实有效。但是,如果我想执行以下操作怎么办……我该怎么做!?:
//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
如果不清楚,请告诉我,我知道一个糟糕的问题是眼睛疼。我会尽快编辑。谢谢。