Backbone.js - 将数据引导到集合中

时间:2013-10-31 10:10:52

标签: javascript backbone.js

我在变量中有以下数据:

[{"age":"65","_id":"526fb6c940301e040e000002","name":{"first":"Jane","last":"Does"},"__v":0,"modified":"2013-10-31T10:03:26.435Z"},{"age":"25","_id":"526fb6c940301e040e000001","name":{"first":"John","last":"Doe"},"__v":0,"modified":"2013-10-31T10:03:26.436Z"},{"age":"45","_id":"526fb6c940301e040e000003","name":{"first":"Alice","last":"Smith"},"__v":0,"modified":"2013-10-31T10:03:26.436Z"}]

我正在尝试用这些数据引导我的收藏。

this.collections.staff = new StaffCollection;
this.collections.staff.reset( data , {parse: true} );

当我console.log(this.collection.staff)时,结果如下:

console.log

我做错了什么?

感谢

2 个答案:

答案 0 :(得分:0)

你必须像这样初始化集合:

this.collections.staff = new StaffCollection(data);

您可以在此JSFiddle中查看它。

干杯。

答案 1 :(得分:0)

也许您可以尝试以下方法:

var json_data = JSON.parse( '{{data variable in String}}' );

this.collections.staff = new StaffCollection( json_data, {parse:true} );