Backbone.js - How to get an array of object literals out of a collection?

professormeowingtons picture professormeowingtons · Apr 8, 2013 · Viewed 14.1k times · Source

I'm trying to keep an array of object literals in sync with server data. These objects are being placed on a Google map via backbone.googlemaps extension.

I have a collection:

var LocationList = Backbone.Collection.extend({ model: Location, url: '/locations' })

How can I grab an array of object literals from this LocationList collection? My goal is such:

[{name: "Home", address: "123 Pleasant St"}, {name: "Work", address: "123 Unpleasant St"}]

Answer

Cubed Eye picture Cubed Eye · Apr 8, 2013

You are looking for the .toJSON() method of the collection, see here:

var locations = new LocationList();

locations.toJSON();