2013-07-01 2 views
2

이 백본 예제에서 항목 변수가 정의되지 않은 이유는 무엇입니까? 여기백본이 각각 정의되지 않음

var Action = Backbone.Model.extend({ 
defaults: { 
    "selected": false, 
    "name": "First Action", 
    "targetDate": "10-04-2014" 
} 
}); 

var Actions = Backbone.Collection.extend({ 
    model: Action 
}); 

var actionCollection = new Actions([new Action(), new Action(), new Action(), new Action()]); 

_.each(actionCollection, function(item) { 
    alert(item); 
}); 

jsFiddle 님의 http://jsfiddle.net/netroworx/KLYL9/

답변

9

변경을 :

actionCollection.each(function(item) { 
     alert(item); 
}); 

그리고 그것은 잘 작동합니다.

actionCollection이 배열이 아니므로 _.each (collection)는 작동하지 않지만 collection.each는 Backbone 컬렉션에 빌드되어 있기 때문에 수행합니다. 상기되는 그게

, 이는 또한 작동 :

_.each(actionCollection.toJSON(), function(item) { 
     alert(item); 
}); 

현재 컬렉션 실제 어레이이므로.

0

_.each은 첫 번째 인수로 배열을 허용하지만 Collection을 전달했습니다.

actionCollection.each(function(item){ 
    //do stuff with item 
}); 
:

는 그냥 Collection.each 방법을 사용
관련 문제