0
Backbone.js에서 데이터를 컬렉션으로 부트 스트랩하려고합니다. 내 부트 스트랩은 다음과 같습니다. log.reset(<JSON>);
백본 컬렉션에 데이터가 있지만 모델이 비어 있습니다.
console.log 내 콜렉션에는 데이터가 가득한 많은 수의 개체가 있습니다. 문제는 루프의 개별 모델에서 console.log를 볼 때 데이터가없는 빈 개체가 나타납니다. 데이터는 모두 컬렉션에 있지만 각 템플릿에 대해 각 패스에 전달할 수는 없습니다. 완벽하게 작동하는 콜렉션 뷰를 렌더링하기 위해 프로젝트의 다른 부분에 똑같은 코드 구조를 사용했습니다 ... 왜 이것이 작동하지 않는지 모르겠습니다.
컬렉션에서 재설정 이벤트를 수신하고 있습니다.
//Log Model
var LogItem = Backbone.Model.extend({});
var logItem = new LogItem();
//Log Collection
var Log = Backbone.Collection.extend({
model: LogItem
});
var log = new Log();
//Log Item View
var LogItemView = Backbone.View.extend({
tagName: 'tr',
template: _.template($('#log-item-template').html()),
render: function(){
var attributes = this.model.toJSON();
this.$el.html(this.template(attributes));
console.log(this.model.toJSON()); //This shows a bunch of empty objects
return this;
}
});
//Log View
var LogView = Backbone.View.extend({
el: '#log',
initialize: function() {
this.collection.on('reset', this.render, this);
},
render: function(){
this.addAll();
console.log(this.collection.toJSON()); //This shows objects full of data
},
addOne: function(food) {
var logItemView = new LogItemView({model: logItem});
this.$el.append(logItemView.render().el);
},
addAll: function() {
this.$el.html('');
this.collection.forEach(this.addOne, this);
}
});
var logView = new LogView({collection: log});
빈 모델을 수정하려면 어떻게해야합니까?
시도
{model: logItem}
로
LogItemView
생성자를 호출합니다. 고맙습니다! 나는 이것이 내게 휴식을 취할 때라고 생각한다. – eablokker@eablokker 실로 :) – nikoshr