所以我已经获取了一个集合并将其传递给视图...
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();
});
请忽略评论......当我返回集合时,在categorias函数console.log(this.collections)
中显然是duh!但是当我console.log(this.collections.models)
返回一个空的 [] 时,.each 不能正常工作......有什么建议吗?谢谢