作者:流浪的牛仔2011Ting_883 | 来源:互联网 | 2023-09-25 21:25
我使用mustache.js作为客户端模板解决方案,但是遇到一个问题,我不知道为什么会这样:
首先,这是我的模板:
{{#steps}}
{{description}}
{{/steps}}
这是我尝试传递的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 =视图对象
目标是我希望更改内容的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);
}
}
解决方法:
我从来没有感到过宽慰的心情,因为我自己解决了我的问题,首先要感谢Tomalak不断关注我的评论并帮助我解决问题.
如果我继续在console.logging上继续工作,我可以早些解决问题,但是我停了下来.
我注意到的一件事是$.get函数给了我#那么,为什么不添加dataType属性呢?
function doJSON(data){
dataSource = data;
if (!regex.test(tpl)) {
$.get(msi.vars.tmpl_url + tpl + '.mustache', render, 'text'); // see here
} else {
render(tpl);
}
}
最终解决了问题.