2014-01-30 71 views
0

我想用數組的partials而且還有數組的主模板。用handlebarsjs管理partials

主要模板:

<div id="notificationsRows" class="wrapper"> 
      {{#each notifications}} 
       {{>notification}} 
      {{/each}} 
     </div> 

行通知:

<div class="box-container-info"> 
    div class="thumbnail" style="background-image: url('{{image}}');"></div> 
    <div class="info">{{message}}</div> 
</div> 

如果我產生主要我可以生成:

var context = { 
    notifications: [{...},{...}] 
} 

這裏沒有問題,但如果我要添加更多通知我不能調用部分數組,如

var moreContextForPartial = [{...},{...}]; // This not works 

我只能添加每一個像一個對象,而不是數組:

var moreContextForPartial = {...}; // This works 

一些想法?

回答

0

你問到的部分 - 這對單行的上下文 - 把你排陣,它不能。 試着問到主模板,誰的迭代器做任務:

var moreNotifications = [{...},{...}]; 
var wrapper = {notifications: moreNotifications }; 

var source = $("#notificationRows").html(); 
var template = Handlebars.compile(source); 

var html = template(wrapper); 
+0

所以,用這種方式,你被軟件寫我可以移動的部分內的各功能,並註冊partil,沒有得到身體的html,這是它的緩慢。 – user1710825

+0

如果你想要,你可以做到這一點,但在這一點上,主模板變得沒用,除非你不寫其他東西...... – MarcoL