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?
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?
Share Improve this question asked Mar 5, 2012 at 15:55 Kevin WhitakerKevin Whitaker 13.4k13 gold badges53 silver badges94 bronze badges 1- 1 Taking a quick look at the annotated source code, it looks like you might try Platforms.toJSON() – Dustin Graham Commented Mar 5, 2012 at 16:01
1 Answer
Reset to default 14you 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/saelfaer/TP9NE/2/