0

In our backbone app, we have a profile model. It has an attribute "completeness" that is an array containing the fields the user has not added to their profile (like profile picture, full name, etc).

In order to easily do listenTo on this array, I'm trying to create a collection to replace the array. My idea is to keep the array in the profile model, and to create a progfileProgress model that gets data from the array in the profile. Then to add this to a collection "Completeness".

How can I juggle the data from the first model into the second collection?

Martin Josefsson
  • 953
  • 12
  • 24

1 Answers1

0

I would do something like this (assuming you have underscore/lodash because you are using Backbone):

// 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));
namero999
  • 2,882
  • 18
  • 24