所以我已经获取了一个集合并将其传递给View ...
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)
它显然返回集合时!但当我console.log(this.collections.models)
给我一个空的[],所以.each不能正常工作......
有什么建议??感谢