5

I have a backbone collection Platforms. The structure of Platforms looks like this:

Platforms
  PlatformList
    models
      0: Platform
        attributes
          id: 1
          name: "some name"
      1: Platform
        attributes
          id: 2
          name: "some other name"

I need to extract the attributes from the models within the collection, and build a JSON array in the following format:

[{"id":1,"name":"some name"},{"id":2,"name":"some other name"}]

Calling Platforms.models.toJSON() or JSON.stringify(Platforms.models) results in a literal string "[[object Object], [object Object]]"

How can I build the JSON array that I need from this collection?

1
  • 1
    Taking a quick look at the annotated source code, it looks like you might try Platforms.toJSON() Commented Mar 5, 2012 at 16:01

1 Answer 1

14

you should not do platforms.models.toJSON() but instead call toJSON on the collection itself!

Platforms.toJSON()

check the following jsfiddle on this solution http://jsfiddle.net/saelfaer/TP9NE/2/

Sign up to request clarification or add additional context in comments.

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.