2013-03-13 51 views
0

我在我的應用程序中有以下代碼。第一次觸發點擊事件模板將按預期添加到頁面中。Mustache.js複製templaer

但是,第二次添加模板的兩個副本,第三次添加三等。所以三次點擊後,我實際上有6個我的模板在DOM內的副本。

即使我將template_view設置爲null後,它已被添加它繼續發生。

有人可以解釋爲什麼會發生這種情況,以及如何避免它?

template_path = "/templates/"; 
var template_html; 

$.get(template_path+"template.html", function(template) { 
    template_html = template; 
});  


$('#test_add').click(function(){ 
    var template_data = {name:'whatever'}; 
    template_view = Mustache.to_html(template_html, template_data); 
    $(body).append(template_view); 
}); 

回答

0

還不確定爲什麼會發生這種情況。最初,雖然我的模板是一個靜態HTML文件,沒有鬍鬚標籤。添加了幾個並傳遞一個數組循環遍歷它現在不再複製模板,每次函數被調用時