2013-05-17 63 views
0

我將collection.toJSON()作爲參數傳遞給underscore模板。在下劃線模板內引用集合變量

render: function() { 
    this.template(this.collection.toJSON()); 
} 

應用路由器內部它被初始化這樣的:

var productsList, products = new Products(); 
var p = products.fetch({ type: 'POST' }); 
p.done(function() { 
    productsList = new ProductsList({ collection: products }); 
    productsList.render(); 
}); 

那我怎麼引用該模板裏面的收藏?

<% _.each(collection, function(p) { %> 
    <tr> 
     <td><%= p.price %></td>   
    </tr> 
<% }); %> 

當我與兩個products/collection變量例外嘗試它提出了說,我用了一個未知的標識符。

回答

1

我認爲你需要改變render()函數。

render: function() { 
    this.template({ 
     collection: this.collection.toJSON() 
    }); 
} 
+0

我用過{products:this.collection.toJSON()};) – lexeme