How to wait the sideloading end before notify a relationship was changed

I have a big performance problem with Ember-data when I have computed properties that depend on relationships.

When an object is loaded from the server, Ember asks the server to load the dependent relationship of the computed property to calculate it.

Ok, that's the good behavior.

But, when I load the relationship with the object (sideloading with JSON API), Ember doesn't wait the end of the relationship (side) load to notify the change!

So, with this computed relation:

my_computed_relation: Ember.computed.filterBy('my_relation', 'attribute', false)

and even if I load "my_relation" with the object, my_computed_relation asks the server to load "my_relation" before the end of the initialization of the object. The relation is loaded twice.

Have you an idea to resolve the performance problem?

(Ember: 1.13.3 - Ember Data: 1.13.12)

Frederic

Upvotes: 0

Views: 50

Answers (1)

Daniel
Daniel

Reputation: 18672

If this computed property happens to be notified in short ammount of time you could use Ember.run.debounce() in observer function and implement filter function by yourself.

For example:

mcrObserver: Ember.observer('my_relation', function() {
  const filterRelation = () => {
    let relation = this.get('my_relation');
    if (!relation) {
      return;
    }

    // ... your implementation of filter

    this.set('my_relation_computed', result); // when you finally have result
  }

  Ember.run.debounce(this, filterRelation, 500); // 500 is time in ms
})

Upvotes: 0

Related Questions