简体   繁体   中英

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

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

var locations = new LocationList();

locations.toJSON();

使用underscore.js pluck方法: http//underscorejs.org/#pluck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM