2013-05-22 141 views
0

我是Handlebars.js的新手。我想知道如何添加一個新項目到現有的車把模板。必須有方法渲染到現有模板的最後一項,對吧?如何將新項目附加到現有的Handlebars.js模板?

看一看my file

謝謝你的時間!

這裏是我想創建魔術的jQuery代碼(用於標記:查看文件)。

<script type="text/javascript"> 
var source = $("#some-template").html(); 
var template = Handlebars.compile(source); 
var data = [ 
    {username: "alan", firstName: "Alan", lastName: "Johnson", email: "[email protected]"}, 
    {username: "allison", firstName: "Allison", lastName: "House", email: "[email protected]"}, 
    {username: "ryan", firstName: "Ryan", lastName: "Carson", email: "[email protected]"} 
]; 
$("#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> 

回答

0

我發現了一個解決這個問題:

我基本上是空着的桌子前,我添加新項DOM。然後我再次渲染整個模板,因爲我知道我的數組已經更新(在幕後)。

謝謝任何​​方式!

這將是招:

$("#content-placeholder").empty(); 
     $("#content-placeholder").append(template(data)); 
相關問題