1
어떤 이유로이 뷰에서 addOne 메소드를 실행할 수 없습니다. render 메서드는 매우 훌륭하게 시작됩니다. 컬렉션은 인스턴스화하는 다른 모듈에서 전달됩니다. 메서드 호출에 괄호를 추가하면 실행되지만 Underscore는 오류를 발생시킵니다.모듈 메소드가()를 사용하여 호출되지 않습니다.
define(['backbone',
'jquery',
'underscore',
'views/user',
'models/user',
],
function(Backbone,$,_,UserView,UserModel){
var Users= Backbone.View.extend({
tagName:'ul',
className:'well',
initialize:function() {
_.bindAll(this);
},
render:function() {
this.collection.each(this.addOne, this);
return this;
},
addOne:function() {
console.log('inside of addOne');
var userView = new UserView({model:UserModel});
this.$el.append(userView.render().el);
}
});
return Users;
});
'this.collection.each'를 사용하면 컬렉션이 비어 있지 않습니까? 그리고 이미 모든 뷰의 메소드를'_.bindAll (this)'로 묶었을 때'each'에 두 번째 인수가 필요하지 않습니다. –
또한, 당신은 아마도'addOne : function (model)'과'new UserView ({model : model})'을 원할 것입니다. –
@muistooshort'자식 {모델 : 배열 [0], 길이 : 0, _byId : 개체, 생성자 : 함수, 모델 : 함수 ...} _byId : Object _idAttr : "id" 길이 : 4 models : Array [4 ] ' 컬렉션이 콘솔에 로깅됩니다. – pelachile