我是 Handlebars.js 的新手。我想知道如何将新项目添加到现有的车把模板。一定有办法将最后一项渲染到现有模板,对吧?
看看我的文件。
谢谢你的时间!
这是我想要创建魔法的 jQuery 代码(对于标记:查看文件)。
<script type="text/javascript">
var source = $("#some-template").html();
var template = Handlebars.compile(source);
var data = [
{username: "alan", firstName: "Alan", lastName: "Johnson", email: "alan@test.com"},
{username: "allison", firstName: "Allison", lastName: "House", email: "allison@test.com"},
{username: "ryan", firstName: "Ryan", lastName: "Carson", email: "ryan@test.com"}
];
$("#content-placeholder").append(template(data));
$('.btn-primary').on('click',
function(){
var thisRow = $('.rows');
var otherContent_1, otherContent_2, otherContent_3;
otherContent_1 = $('#firstModal').find('input:eq(0)').val();
otherContent_2 = $('#firstModal').find('input:eq(1)').val();
otherContent_3 = $('#firstModal').find('input:eq(2)').val();
var newObj = {username: otherContent_1, firstName: otherContent_2, email: otherContent_3}
data.push(newObj);
/*
here is the problem how do I append/render this last-item to the template?
*/
console.log(data.length); // I know that I've 4 items.
$('#firstModal').modal('hide');
});
</script>