Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.JS: Observing @each, but just iterating over new/changed items

I'm currently observing some Ember arrays like so:

 observe_array: function() {
     this.get("my_array").forEach(function(e,i) {
         // do something
     });
 }.observes("my_array.@each");

Most times, if my_array is updated, multiple elements are added at once. However, the observer fires one-by-one as each element is added, which becomes extremely inefficient. Is there anyway to do this more efficiently? Essentially, I need to be able to have a mutated array based on "my_array"

For reference, realistic sizes of my_array will be between 600-1200 elements. The "do something" block involves some operations that take a little more time - creating Date objects from strings and converting each element to json representation.

Instead of doing an observer I also tried a property with the cacheable() method/flag, but that didn't seam to speed things up very much....

like image 819
Conrad Vanlandingham Avatar asked Oct 29 '12 15:10

Conrad Vanlandingham


1 Answers

Assuming (via comments) that your array is an ember-data populated one, you should try observing array.isUpdating property. I got success w/ this one.

The only drawback is it is only set when using .findAll()! (so Model.find())

like image 173
Mike Aski Avatar answered Nov 08 '22 18:11

Mike Aski