뷰에서 렌더링하려는 백본 컬렉션이 있습니다. JSON 데이터가 올바른 것처럼 보이지만 뷰 내에서 값에 액세스 할 수 없습니다.Backbone.js 렌더링 컬렉션보기
define(['backbone', 'BaseModel'], function(Backbone, BaseModel) {
var BaseCollection = Backbone.Collection.extend({
model: BaseModel,
url: "/collection/get?json=true",
initialize: function() {}
});
return BaseCollection;
});
여기보기입니다 : 내가 컬렉션 내의 각 모델에 대한 this.render
을 반복 할 필요가 있다고 생각
define(['backbone', 'BaseCollection'], function(Backbone, BaseCollection) {
var BaseView = Backbone.View.extend({
el: $('#baseContainer'),
template: _.template($('#baseTemplate').html()),
initialize: function() {
_.bindAll(this);
this.collection = new BaseCollection();
this.collection.bind('all', this.render, this);
this.collection.fetch();
},
render: function() {
//This returns 3 objects which is correct based on the JSON data being returned from the server
console.log(this.collection.toJSON());
var html = this.template(this.collection.toJSON());
this.$el.html(html);
return this;
},
});
return BaseView;
});
다음은 기본 모음입니다. 그러나 모든 반복을 완료 할 때까지 '렌더링'해서는 안되기 때문에 확실하지 않습니다.
어떤 제안이 좋을까요? 고맙습니다!
템플릿을 표시 할 수 있습니까? 내 생각 엔'this.template()'에 전달할 매개 변수를 수정하거나 템플릿 자체를 수정할 필요가있다. 템플릿 내에서'collection.each'를 사용하여 컬렉션의 모든 모델을 반복 할 수 있습니다. (모델 별 이벤트를 많이 묶을 경우 각 모델에 대한 하위 뷰를 만드는 것이 좋습니다) – jackwanders