1

Having a problem where if I do

apples = new Apples
apples.fetch()
console.log apples
console.log apples.models

console.log apples will out put the Apples Collection, with models: Array(10) listed inside the object.

But, console.log apples.models outputs an empty array.

Why does this happen?

Thanks in advance.

  • Are you trying to console.log the collection right after executing the fecth() method or waiting for the callback to be executed by using apples.on('reset', function(){ console.log(this.models} } ,this); ? – Shaked KO Aug 14 '12 at 14:35
  • Not waiting; But now see this as the problem; thank you! If you want to post this as an answer I'll accept it –  Aug 14 '12 at 14:39
  • Check http://stackoverflow.com/questions/9911637/backbone-js-model-get-returning-undefined-even-though-i-can-see-the-attribut and http://stackoverflow.com/questions/11459244/backbone-js-empty-array-attribute/11463190#11463190 – fguillen Aug 14 '12 at 14:43

2 Answers2

0

I just tried this and it worked out for me. Backbone collections have a few underscore methods which work out of the box. Try using toArray to get the apple models in the apples collection.

  apples.toArray()
Khallil Mangalji
  • 471
  • 5
  • 13
0

Are you trying to console.log the collection right after executing the fecth() method or waiting for the callback to be executed by using

apples.on('reset', function(){ console.log(this.models);} ,this); 

?

Kyle Hale
  • 7,912
  • 1
  • 37
  • 58
Shaked KO
  • 399
  • 2
  • 11