2013-03-14 97 views
0

我有一個div容器元素,在這個容器元素中我有多個模板。我想根據從後端返回的json有選擇地加載模板。這是一個微不足道的問題,但面臨着這些問題。一些邏輯代碼。Backbone.js視圖+ underscore.js模板

<div id = "container"> 
    <div class = "row page"> 
    <script type = "text/template" id = "template1"> 
     <div id = "template1_id"> 
     </div> 
    </script> 
    <script type = "text/template" id = "template2"> 
     <div id = "template2_id"> 
     </div> 
    </script> 
    </div> 
</div> 

在我的骨幹查看我在做這樣的事情:

var someView = Backbone.view.extend({ 
    el: '.page' 
    render: function() { 
    el: '.page'; 
    var template1 = _.template($('#template1').html()); 
    this.$el.html(template1); 
    //get Json from backend and render the template within the fetch method 
    var collection1 = new someCollection(); 
    someCollection.url = "blah"; 
    someCollection.fetch (function() { 
     success: function() { 
     var template2 = _.template($('#template2').html()); 
     $('#template1_id').html(template2); 
     } 
    }); 

    }, 
}); 

只有第一個模板被渲染,而不是第二個。我在這裏做一些根本性的錯誤嗎?

回答

1

如果你正在呈現兩者的模板到一個容器中,你應該使用$.append,而不是$.html,因此兩者都將被附加。

+0

謝謝你。對我來說太愚蠢了。 – 2013-03-14 08:20:15

0

你有一個錯字(不匹配報價)

$('#template1_id").html(template2); 

應該

$("#template1_id").html(template2); 
+0

嘿謝謝 - 我解決了,但問題仍然存在。我們在講話時正在調試。 – 2013-03-14 08:00:10