2011-12-07 7 views
9

したがって、私はbackbone.jsを学習しており、現在、以下の例のビューで一部のモデルを反復処理しています。最初のスニペットは動作しますが、他のアンダースコア.jsベースのスニペットは動作しません。どうして?underscore.jsでオブジェクトを反復する

// 1: Working 
this.collection.each(function(model){ console.log(model.get("description")); }); 

// 2: Not working  
_.each(this.collection, function(model){ console.log(model.get("description")); }); 

私は自分で見ることができないので、私は間違っていますか? this.collection.eachコレクションインスタンスの.models性であるカバーの下に適切なオブジェクトを反復処理する方法である

this.collection
+2

*何か*起こりますか?コンソールにエラーがありますか? – Pointy

+0

#2は、コンソールへの出力なしでサイレントモードで実行されます。 – Industrial

答えて

22

インスタンスです。これにより

はあなたが試すことができます言った:this.collection.eachとして完全に無意味です

_.each(this.collection.models, function(model){ console.log(model.get("description")); }); 

は次のように行う関数である:だからあなたにもthis.collection.eachを使用する場合があります

function(){ 
return _.each.apply(_, [this.models].concat([].slice.call(arguments))); 
} 

; P

+1

解決策と一緒に機能しなかった理由を説明してくれてありがとう! – Industrial

2

また、お試しいただけます。

_.each(this.collection.models, function(model){ 
    console.log(model.get("description")); 
}); 
関連する問題