0

我有我的看法:

var ProductsView = Backbone.View.extend({

    initialize: function(){
        var that = this;
        this.collection.fetch({
            success: function()
                {
                    console.log("fetcheo");
                    that.render();
                }
        });

        this.listenTo(this.collection, "reset", this.render);
    },

    render: function(){

        var cats = [];
        this.collection.each(function(model)
                            {
                                cats.push(model.get('familia'));
                            });
        this.cats = _.uniq(cats, false);
            console.log(this.cats) // It returns ["VINOS", "CERVEZA", "BOTANA"]
        this.$el.html(Handlebars.templates.products(this.cats));
        return this;
    }
});

这是预编译的 Handlebar 模板:

<h1>Y LOS MODELOS SON</h1>
<ul>
{{#each cats}}
<li>
{{this}}
</li>
{{/each}}
</ul>

但它没有渲染 this.cats 数组;这不是收集问题(),我已经解决了一个早期问题。谢谢您的帮助...

4

1 回答 1

1

您只需要将对象包装在“cats”属性中:

this.cats = { cats: _.uniq(cats, false) }

请注意,当您使用 时{{#each cats}},渲染器将查找名为“cats”的属性。您的变量名称是“cats”,但渲染器根本看不到它。

小提琴演示

于 2013-10-24T01:37:50.953 回答