我有一個稱爲主題的模型。 主體的一個屬性稱爲接收者,由一組對象組成。 我想從對象數組中創建一個集合。從嵌套在模型中的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