如何在骨干网中将数组转换为Collection [英] How to convert an array to Collection in backbone
问题描述
我有一个定义为的数组:
I have an array defined as:
this.noOfHouseHold = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"];
我正尝试将其转换为Backbone Collection,例如:
I'm trying to convert it to a Backbone Collection as:
var adultListCollection = new Backbone.Collection(this.noOfHouseHold);
它给出了我的列表,但对于2位数字,它显示如下内容:
It gives me the list but for 2 digit numbers, it shows something like this:
attributes: Object
0: "1"
1: "1"
我无法理解这里出了什么问题,或者是将数组转换为集合的错误方法.请提出建议.预先感谢!
I'm not able to understand as to what is wrong here or is it the wrong way I'm converting my array to collections. Please suggest. Thanks in advance!
推荐答案
Backbone集合需要一个模型/哈希表列表,这些列表可以转换为模型,但是您具有纯数组.
A Backbone collection expects a list of models/hashes of attributes that can be converted to a model but you have a plain array.
您将必须将数组转换为哈希列表.假设您的值是模型的id
:
You will have to transform your array to a list of hashes. Assuming your values are the id
s of your models :
var lst = _.map(this.noOfHouseHold, function(val) {
return {id: val};
});
var adultListCollection = new Backbone.Collection(lst);
这篇关于如何在骨干网中将数组转换为Collection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!