9

Using backbone.js and trying to get data from postsList array I got this in chrome console.log

d {length: 0, models: Array[0], _byId: Object, _byCid: Object, constructor: function…}
  _byCid: Object
  _byId: Object
  length: 9
  models: Array[9]
  __proto__: f

When I'm trying to use console.log(postsList.length) I get 0, but there are 9 models inside. I don't know how to get number of them.

Gediminas Šukys
  • 7,101
  • 7
  • 46
  • 59
  • postsList.length should give you 9, are you sure you are calling it after the models are added to the collection? open the chrome console and try postsList.length and see what u get – Bassam Mehanni Jan 25 '13 at 18:11
  • What's the code leading up to this point? – Bryan A Jan 25 '13 at 18:25

2 Answers2

12

Yes, this is strange behaviour :)

Chrome displays object preview immediately after you have used console.log. When you entered console.log(collection) it was empty (probably you have fetched model from the server). But at the moment when you expand the object in console Chrome displays actual object params at current moment.

Try this in console:

var object = {key1:{prop:true}};
console.log(object)
object.key2 = true;
console.log(object)

To get collection length use this way:

collection.fetch({context:collection}).done(function() {
  console.log(this.length)
});

EDIT

No-no-no :) Use this.length instead of this.lenght.

collection.fetch({context:collection}).done(function() {
    // equals to this.length
    console.log(this.size());
    // get all models from collection (array of Backbone.Models)
    console.log(this.models);
    // get all models from collection (like simple array of objects)
    console.log(this.toJSON());
    // get model with index 1
    console.log(this.at(1));
    // get model data with index 1
    console.log(this.at(1).toJSON());
    // get model with id `some-id`
    console.log(this.get('some-id'));
    // get models data where property `id_str` equals to `292724698935070722`
    console.log(this.where({id_str:'292724698935070722'}));
});

For more information look here: http://backbonejs.org/#Collection

Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55
  • Thanks it really works! But there are some problems. Sometimes this.lenght value is udefined even if models are in place. And I got some errors on repeating same conde blocks again. Maybe you can help me with that "d" array. I really don't understand how it works and how to get something from it, because it has everythink. Can I receive params directly from it? – Gediminas Šukys Jan 26 '13 at 10:55
  • @Lucas take a look on explanation below `EDIT`. Hope this will help you. – Vitalii Petrychuk Jan 26 '13 at 11:38
3

I think Vitaliys answer is a little bit dangerous, because the passed option {context: collection} is:

  • a) not mentioned in backbones documentation
  • b) handled deep down in jQuery when fetch triggers some Ajax-call.

Instead the length of the fetched collection can be easily checked in the success- and error- callback of fetch, e.g.:

someCollection.fetch({
    success: function(collection) { // the fetched collection!
        if (collection.length) {
            // not empty
        } else {
            // empty
        }
    }
});

See http://backbonejs.org/#Collection-fetch

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
bgraves
  • 788
  • 1
  • 11
  • 23
  • The [context](http://backbonejs.org/docs/backbone.html#section-81) option isn't well documented but is clearly intended to be used. `if (success) success.call(options.context, model, resp, options);` In the case of this answer, you're right that the context isn't necessary. – Emile Bergeron Apr 20 '16 at 19:59