here에 위치한 BackboneJs에서 찾은 자습서를 따르고 있습니다. 반쯤 그가 다음을 수행하는 render
방법에 대해BackboneJs 튜토리얼 질문
:
events: { 'click button#add': 'addItem' },
initialize: function() {
this.collection = new List();
// Collection event binder
this.collection.bind('add', this.appendItem);
this.counter = 0;
this.render();
},
render: function() {
this.el.append("<button id='add'> Add List Item</button>");
this.el.append("<ul></ul>");
_(this.collection.models).each(function(item){
// in case collection is not empty
appendItem(item);
}, this);
},
addItem: function() {
var item = new Item();
this.counter++;
item.set({
part2: item.get('part2') + " " + this.counter
});
this.collection.add(item);
},
appendItem: function (item) {
$('ul').append('<li>' + item.get('part1') + " " + item.get('part2') + '</li>');
}
나는 아래 라인에 대한 몇 가지 질문이 있습니다.
_(this.collection.models).each(function(item){
// in case collection is not empty
appendItem(item);
}, this);
이 문맥에서 밑줄 _
은 무엇을합니까?
왜 이것이 필요한가요?
댓글에 컬렉션이 비어 있지 않은 경우를 말합니다. 그러나 그 라인이 없으면 잘 작동합니다. initialize
함수의 bind
재정의는 add
이벤트가 컬렉션에서 트리거 될 때 this.appendItem
을 실행하도록 Backbone에 알려주므로 문제의 라인을 제거하여 생각하고 확인했습니다.
Perfect. 감사. 나는 언더 스코어 (underders)가 의존성이라는 것을 완전히 잊어 버렸습니다. 아마도 그것이 백본 일이라고 생각했습니다. –