我的javascript控制台中有一个看起来像这样的对象:
r {length: 1, models: Array[1], _byId: Object, constructor: function, model: function…}
_byId: Object
length: 1
models: Array[1]
0: r
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
collection: Array[20]
created_at: Wed Mar 27 2013 03:24:31 GMT-0400 (Eastern Daylight Time)
__proto__: Object
changed: Object
cid: "c26"
collection: r
__proto__: s
length: 1
__proto__: Array[0]
__proto__: s
我应该在课堂上注意......但我怎样才能访问“collection:Array [20]”?有办法吗?
答案 0 :(得分:1)
// Get array
r.models[0].attributes.collection
应该让您进入属于models数组中第一个模型的集合。
如果您想在数组中使用单个元素: -
// Get first element of collection on first model
r.models[0].attributes.collection[0]
答案 1 :(得分:1)
它看起来像backbone.js集合,所以你可以通过这样做到达那个数组:
// Get model by index in collection
var collection = r.at(0).get('collection')
// Or get model by client id (cid)
var collection = r.getByCid('c26').get('collection')