2011-06-30 44 views
1

我有以下Backbone.js的客戶端模板:綁定模型到主幹客戶端模板

<script id="calleeTemplate" type="text/x-jquery-tmpl"> 
<tr style="background-color: ${StatusColour}"> 
    <td class="responder">${ContactFullName}</td> 
    <td class="status" style="width:200px">${Status}</td> 
    <td class="replied">${Replied}</td> 
    <td class="wauto">${Response}</td> 
</tr> 
</script> 

爲了能夠結合到這些性質,我有以下呈現視圖的方法:

App.Views.Callees = Backbone.View.extend({ 
    initialize: function() { 

    }, 
    el: $('#calleesStatuses'), 
    render: function() { 
    var col = _.map(this.collection.models, function (item) { 
     return item.attributes; 
    }); 

    $('#calleesStatuses').html(''); 
    $('#calleeTemplate').tmpl(col).appendTo(this.el); 
    } 
}); 

我必須使用模型中的下劃線_.map函數提取屬性。我認爲這是因爲骨幹使用.get("property")來提取屬性值。

這似乎對我來說不對,我是否做錯了什麼?

回答

2

你是對的,你必須改變數據,以便能夠輕鬆地將它與tmpl一起使用。

但是,最好使用toJSON而不是直接訪問屬性。最好避免直接撥打.models

無論如何,骨幹集合都有一整套underscore.js枚舉器。因此,您可以將您的轉換切割爲一條線:

var col = this.collection.invoke('toJSON')