クライアント側のテンプレートソリューションとしてmustache.jsを使用していますが、なぜこれが発生するのかわからないという問題が発生しました。
まず、これが私のテンプレートです:
<ul>
{{#steps}}
<li>
<a href="{{url}}">
<div class="step_visual {{step_status}}">
<span class="step_description">{{description}}</span>
</div>
</a>
</li>
{{/steps}}
</ul>
これが私が渡そうとしているjsonオブジェクトです:
var view = {
"steps": [
{
"url": "complete_profile",
"step_status": "done",
"description": "Complete Proflie"
},
{
"url": "complete_form1",
"step_status": "active",
"description": "Submission of Form 1"
}
]
}
そして、このスクリプトを実行して、アプリでレンダリングしてみました
var content = Mustache.render(template, view);
$(target).html(content).hide().fadeIn();
ここでtemplate
=上記のテンプレート|
view
=ビューオブジェクトでありtarget
、コンテンツを変更したいhtml要素です
私を困惑させるのは、私のテンプレートが次のようになっているときにうまく機能することです
{{#steps}}
{{url}}
{{description}}
{{step_status}}
{{/steps}}
または、テンプレートにループするものがない場合。
バグは何でしょうか?口ひげのバグだと思いますか?
編集 私がコードが機能するのを見たように、
関数を呼び出す方法に問題があるのでしょうか?
tmplReplaceContent : function(json, tmpl, target) {
var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}"),
view = '';
function setOutput(template) {
var content = Mustache.render(template, view);
$(target).html(content).hide().fadeIn();
}
function doJSON(json) {
view = json;
if(!regex.test(tmpl)){
/* get mustache tmpl from the path */
$.get(msi.vars.tmpl_url + tmpl + '.mustache', setOutput);
} else {
setOutput(tmpl);
}
}
/* json -> check if object */
if (typeof json === 'object') {
doJSON(json);
} else {
/* getJSON from the path */
$.getJSON(msi.vars.base_url + json, doJSON);
}
}