BackBone集合将一系列模型作为单个模型?

时间:2013-11-15 10:42:14

标签: backbone.js collections model models

在下面的代码中

 var productcollection = new Products([this.$results]);
 alert(productcollection.length);

这里。$ results包含一个更像这样的字符串

{"ProdID":"98","ProdName":"product1","ProdPic":"null","ProdGist":"null","ProdDesc":"null","ProdCat":"","ProdTech":"","LastModified":""},{"ProdID":"928","ProdName":"product21","ProdPic":"null","ProdGist":"null","ProdDesc":"null","ProdCat":"","ProdTech":"","LastModified":""}

但是集合的长度返回为“1”。我应该对我给予该集合的模型数据进行哪些更正。所以它返回“2”。

1 个答案:

答案 0 :(得分:2)

如果你真的有一个字符串。$ results试试这个:

var array = JSON.parse('['+this.$results+']');
var productcollection = new Products( array );

您的代码正在创建一个包含一个大字符串的数组,例如:

new Products( ["text"] );