Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"}]

like image 581
professormeowingtons Avatar asked Apr 07 '13 23:04

professormeowingtons


2 Answers

use underscore.js pluck method: http://underscorejs.org/#pluck

like image 71
Eru Avatar answered Sep 28 '22 13:09

Eru


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

var locations = new LocationList();

locations.toJSON();
like image 40
Cubed Eye Avatar answered Sep 28 '22 15:09

Cubed Eye