title: Backbone: Access Collection Models
body: <p>This question has been asked several times, but none of the solutions offered are working for me.</p>
  
  <p>In this Backbone collection, how can I access and loop through its models?</p>
  
  <p>I've tried several ways in the code below; includes additions based on Mark V.'s answer.</p>
  
  <p>Code is also available here: <a href="http://jsfiddle.net/LPbsP/3/" rel="nofollow noreferrer">http://jsfiddle.net/LPbsP/3/</a></p>
  
  <pre><code>(function() {
  
  console.log(Backbone);
  
  window.App = {
      Model: {},
      Collection: {},
      View: {}
  };
  
  App.Model.Whatever = Backbone.Model.extend({});
  
  App.Collection.Whatever = Backbone.Collection.extend({
      model: App.Model.Whatever,
  
      initialize: function(models, options) {
          this.getModels();
  
          _.bindAll(this, 'getModelsWithBindAll');
          this.getModelsWithBindAll();
  
          console.log(this);
          console.log(models);
          models.each(function(model) {
              console.log(model);
          });
      },
  
      getModels: function() {
          console.log('in getModels');
          console.log(this);
  
          whateverCollection.each(function(model) {
              console.log(model);
              console.log(model.toJSON());
          });
      },
  
      getModelsWithBindAll: function() {
          console.log('in getModelsWithBindAll');
          console.log(this);
  
          whateverCollection.each(function(model) {
              console.log(model);
              console.log(model.toJSON());
          });
      }
  });
  
  var whateverCollection = new App.Collection.Whatever([
      {
          name: 'jim',
          title: 'boss'
      },
      {
          name: 'tom',
          title: 'worker'
      }
  ]);
  
  console.log('program code');
  console.log(whateverCollection);
  
  })();
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>Object (Backbone)
  
  in getModels
  
  r (length: 0, models: Array[0] ... )
  
  Cannot call method 'each' of undefined
  </code></pre>
  
  <hr>
  
  <p>Here are the other questions I referenced:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/q/14057812/723007">trying to loop through collection in backbone to output attributes in DOM</a></p></li>
  <li><p><a href="https://stackoverflow.com/q/14853997/723007">collection.each() does not iterate over models</a></p></li>
  <li><p><a href="https://stackoverflow.com/q/16350374/723007">Cant loop over backbone collection</a></p></li>
  <li><p><a href="https://stackoverflow.com/q/11726943/723007">For Loop over Backbone Collection</a></p></li>
  </ul>