简体   繁体   English

Backbone.js - 如何从集合中获取对象文字数组?

[英]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. 这些对象通过backbone.googlemaps扩展程序放置在Google地图上。

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? 如何从此LocationList集合中获取对象文字数组? 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 : 您正在寻找集合的.toJSON()方法, 请参见此处

var locations = new LocationList();

locations.toJSON();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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