0
我正在与backbone.js玩弄,并陷入僵局。这里是我的代码:隐藏Backbone.js模型
window.Car = Backbone.Model.extend();
window.CarCollection = Backbone.Collection.extend({
model: Car,
url:"../api/car/",
});
window.CarListView = Backbone.View.extend({
tagName:'ul',
initialize: function() {
this.model.on("reset", this.render);
},
render: function (eventName) {
console.log(this.model); //is an object
console.log(this.model.models); //is an empty array
return this;
},
});
// Router
var AppRouter = Backbone.Router.extend({
routes:{
"":"list",
"cars/:id":"carDetails"
},
list:function() {
this.carList = new CarCollection();
this.carListView = new CarListView({model:this.carList});
this.carList.fetch();
$('#sidebar').html(this.carListView.render().el);
},
});
$(function() {
app = new AppRouter();
Backbone.history.start();
});
我没有足够的信誉发表图片,所以这里是从Chrome开发人员工具控制台的文本版本:
V r {length: 0, models: Array[0], _byId: Object, _events: Object, constructor: function…}
> _byId: Object
> _events: Object
> length: 4
> models: Array[4]
> __proto__: s
我的问题是,我试图访问似乎“隐藏”在空白背后的非空模型属性。如何访问长度为4的模型属性?
感谢,有两件事:1:该代码导致'未捕获的ReferenceError:carListView未定义'。 2:我认为绑定'重置'事件会导致渲染函数在数据被提取后被调用? –
1.哎呀,你是对的 - 看看编辑。您需要将视图别名为变量,因为'this'引用在回调中不起作用。 2.'reset'监听器应该可以工作,但是在你调用'.fetch()'之后,你首先明确地调用'carListView.render()'。 – nrabinowitz