Skip to content

Instantly share code, notes, and snippets.

@AmilKey
Created October 7, 2015 14:14
Show Gist options
  • Select an option

  • Save AmilKey/e14710601dc1757e78a0 to your computer and use it in GitHub Desktop.

Select an option

Save AmilKey/e14710601dc1757e78a0 to your computer and use it in GitHub Desktop.
Ember forEach aaray
https://github.com/emberjs/data/issues/772
I think there is a kind of Conccurent modification, you're walking through the list
and delete one element at the same time. I think the run.once defers the deletion after the loop has been made.
To avoid it, I think you could iterate over a copy instead of the content. Something like
@get("content").toArray().forEach(article) ->
article.deleteRecord()
forEach uses Array.prototype.forEach which is basically:
Array.prototype.forEach = function(fn, scope) {
for(var i = 0, len = this.length; i < len; ++i) {
fn.call(scope, this[i], i, this);
}
}
That means if you delete an item as you're iterating through then the indexes won't match up anymore as this.length has changed.
Subsequent items will be at different positions and the last item will be undefined (the error you saw)
A common work-around to this is to loop through the list backwards as deleting items doesn't affect the indexes
of previous items in the list. Would be handy to have a forEachInReverse or something!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment