【问题标题】:Referencing collection variable inside underscore template在下划线模板中引用集合变量
【发布时间】:2013-05-17 21:21:42
【问题描述】:

我将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 变量尝试它时,出现异常说我使用了未知标识符。

【问题讨论】:

    标签: javascript jquery backbone.js underscore.js


    【解决方案1】:

    我认为您需要更改 render() 函数。

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

    【讨论】:

    • 我用过 {products: this.collection.toJSON()} ;)
    猜你喜欢
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多