2013-08-05 88 views
0

在我们的骨干应用程序中,我们有一个配置文件模型。它有一个属性“完整性”,即包含用户未添加到其个人资料中的字段的数组(如个人资料图片,全名等)。如何从骨干模型属性创建集合?

为了方便地在此数组上执行listenTo,我试图创建一个集合来替换数组。我的想法是将数组保存在配置文件模型中,并创建一个从配置文件中的数组获取数据的progfileProgress模型。然后将其添加到“完整性”集合中。

我该如何将第一个模型中的数据转换为第二个集合?

回答

0

我会做这样的事情(假设你有因为你使用的骨干下划线/ lodash):

// Here I build a data structure from the completeness array. 
// It will be the model of the new Collection. 
var missingFields = _.map(profileModel.get('completeness'), function(el) { 
    return {missing: el}; 
}); 

// The new collection that you can set on your model and do listenTo 
profileModel.set('profileProgress', new Backbone.Collection(missingFields));