我有一个称为主题的模型。 主体的一个属性称为接收者,由一组对象组成。 我想从对象数组中创建一个集合。从嵌套在模型中的JSON数组填充集合
在主题视图我这样做:
var receivers = this.model.get('receivers');
,一切看起来不错在控制台中。如预期的那样,接收者变量现在由五个对象组成。
那么我这样做:
var receiversCollection = new App.Collections.Receivers(receivers );
现在,当我检查receiversCollection它包含了只有两个奇怪的物体:
child {length: 2, models: Array[2], _byId: Object, constructor: function, model: function…}
我在做什么错?
编辑 - 添加下列信息。
这是我的定义:
App.Models.Receiver = Backbone.Model.extend({
defaults: {
first_name: '',
last_name: '',
email: '',
location: '',
created_at: null,
updated_at: null
}
});
App.Collections.Receivers = Backbone.Collection.extend({
model: App.Models.Receiver
});
这是5个接收器看起来像在控制台什么:
0: Object
created_at: "0000-00-00 00:00:00"
email: "[email protected]"
first_name: "Adam"
id: "1"
last_name: "Bertilsson"
location: "Stockholm"
pivot: Object
updated_at: "2013-04-16 07:33:26"
__proto__: Object
1: Object
created_at: "0000-00-00 00:00:00"
email: "[email protected]"
first_name: "Sven"
id: "2"
last_name: "Svensson"
location: "Uppsala"
pivot: Object
updated_at: "0000-00-00 00:00:00"
__proto__: Object
2: Object
created_at: "0000-00-00 00:00:00"
email: "[email protected]"
first_name: "Adam"
id: "1"
last_name: "Bertilsson"
location: "Stockholm"
pivot: Object
updated_at: "2013-04-16 07:33:26"
__proto__: Object
3: Object
created_at: "0000-00-00 00:00:00"
email: "[email protected]"
first_name: "Adam"
id: "1"
last_name: "Bertilsson"
location: "Stockholm"
pivot: Object
updated_at: "2013-04-16 07:33:26"
__proto__: Object
4: Object
created_at: "0000-00-00 00:00:00"
email: "[email protected]"
first_name: "Sven"
id: "2"
last_name: "Svensson"
location: "Uppsala"
pivot: Object
updated_at: "0000-00-00 00:00:00"
__proto__: Object
length: 5
__proto__: Array[0]
这就是receiversCollection看起来像在控制台:
_byId: Object
1: child
2: child
c56: child
c57: child
__proto__: Object
length: 2
models: Array[2]
0: child
1: child
length: 2
__proto__: Array[0]
__proto__: Surrogate
发布您的JSON,也许'App.Collections.Receivers'的定义。 – Loamhoof 2013-04-22 08:59:01
好吧,我已经编辑了我的问题,并包含了定义以及对象的外观。 – dannepanne 2013-04-22 09:48:57
你可以发布你期望的阵列的整个JSON。我的意思是所有的5个模型 – 2013-04-22 09:49:56