我是Mustache和模板的新手。我试图遍历我的数组对象,并使用胡子和ajax在dom上显示它们。截至目前,内容未传递给内容。
这是我的剧本:
var ajax = function(){
alert('crackers');
var request1 = $.ajax({
url: "js/projects.js",
dataType:'json',
}).then(function(response){
var data = response.projects;
var template = $('#projects_tmp').html();
var project = Mustache.to_html(template, data);
return project;
});
var request2 = $.ajax({
url: "js/projects.js",
dataType:'json',
}).then(function(response){
var data = response.projects;
var template = $('#projectHeader_tmp').html();
var projectHeader = Mustache.to_html(template, data);
//console.log(projectHeader);
return projectHeader;
});
$.when(request1, request2).then(function(t,r) {
$('.projects').html(t).prepend(r);
});
}
$("#header").on('click', ajax);
}());
我尝试使用{{#projects}} [template] {{/ projects}},但没有显示任何内容。如果我删除它的标签,但只显示模板的一个项目,当它们应该是14。 这是我的模板:
<script id="projects_tmp" type="text/template">
<div class='small-6 medium-3 columns left thumb {{{genre}}}'>
<div class='project'><figure><img src='{{{largePic}}}' alt='' />
<figcaption class='caption'><span>{{genre}}</span></figcaption class='caption'></figure></div>
</div >
</div >
</script>
答案 0 :(得分:0)
答案:
var ajax = function(){
var request1 = $.ajax({
url: "js/projects.js",
dataType:'json',
}).then(function(response){
var data = response.projects;
var template = $('#projects_tmp').html();
var projects;
$.each(data, function(key,val){
projects += Mustache.to_html(template, val);
return projects;
});
var project = Mustache.to_html(template, data);
return projects;
});
var request2 = $.ajax({
url: "js/projects.js",
dataType:'json',
}).then(function(response){
var template = $('#projectHeader_tmp').html();
var projectHeader = Mustache.to_html(template);
return projectHeader;
});
$.when(request1, request2).then(function(t,r) {
$('.projects').html(t).prepend(r);
});
}
$("#header").on('click', ajax);