Looping over collections


#1

I recently bumped into a problem.

I have a computed property that filters my models based on their status.

isUnread: Ember.computed.filterBy('model', 'status', 'unread')

Now, when I click a “Mark all as read” button and loop over the collection, only half the models get changed. I suspect it has to do with the forEach counter that gets updated and thus only completed half the models.

this.get('isUnread').forEach(function(message) {
    message.set('status', 'read');
}

Does any of you know how to get around this issue?


#2

Typical. After two minutes of posting this issue, I found a solution after 2 hours of debugging it.

Creating a copy of the collection seems to do the trick.

var messages = this.get('isUnread').copy();
messages.forEach ...