0

I have an object that looks like this in my javascript console:

r {length: 1, models: Array[1], _byId: Object, constructor: function, model: function…}
    _byId: Object
    length: 1
    models: Array[1]
        0: r
            _changing: false
            _events: Object
            _pending: false
            _previousAttributes: Object
            attributes: Object
                collection: Array[20]
                created_at: Wed Mar 27 2013 03:24:31 GMT-0400 (Eastern Daylight Time)
                __proto__: Object
            changed: Object
            cid: "c26"
            collection: r
            __proto__: s
            length: 1
        __proto__: Array[0]
    __proto__: s

I should have paid attention in class... but how can I access that "collection: Array[20]"? Is there a way?

0

2 Answers 2

1
// Get array
r.models[0].attributes.collection

Should get you to the collection belonging to the first model in the models array.

If you want an individual element in the array:-

// Get first element of collection on first model
r.models[0].attributes.collection[0]
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. I was doing r.models[0].collection and it kept me going in circles!
My top tip for "Javascript discovery" ( we all have to do it ) is to play with the Chrome debugger. Really teaches you a lot.
Yep, that's where I was doing it. Thanks.
1

It looks like backbone.js collection so you can get to that array by doing this:

// Get model by index in collection
var collection = r.at(0).get('collection')

// Or get model by client id (cid)
var collection = r.getByCid('c26').get('collection')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.