0

だから私はすでにコレクションを取得してビューに渡しています...

var ProductsView = Backbone.View.extend({

    initialize: function(){
        console.log(this.collection); // Here shows me data...
        console.log(this.collection.models);// But here shows me []?????????
        this.listenTo(this.collection, "reset", this.render);
        this.listenTo(this.collection, "change", this.render);
        this.categorias();
    },

    categorias: function(){
        this.cats = [];
        this.collection.each(function(model)
                            {
                                console.log(model.get('familia'));
                                //this.cats.push(model.get('familia'));
                            });
        //return _.uniq(this.categorias,false);
    },

    render: function(){
        var cat = _.uniq(this.cats, false);
        this.$el.html(Handlebars.templates.products(cat));
        return this;
    }
});

ルーターを入れましょう:

var AppRouter = Backbone.Router.extend({

    routes:{
        "" : "productos",
    },

    initialize: function(){
        this.productoItems = new Productos();
        this.productoItems.fetch({reset:true});

        //this.productoItem = new Producto();
        //Crear la vista del detalle de un producto     

        this.productosView = new ProductsView({collection: this.productoItems});
    },

    productos: function(){
        $('#app').html(this.productosView.render().el);
    }

});

/*********************************/
var app = new AppRouter();

$(function(){
    Backbone.history.start();
});

console.log(this.collections)コメントを無視してください...明らかにコレクションを返すときのcategorias関数で!しかしconsole.log(this.collections.models)、空の[]を返すと、.eachが正しく機能しません...何か提案はありますか?? ありがとう

4

0 に答える 0