What are the advantages of async relationships?


#1

First of all, this is an honest question. If there are advantages in using async relationships, I’m just failing to see them.

Handling the promises in templates is easy thanks to Handlebars, which is smart enough to get a property from the resolved value of a promise instead of the promise itself:

{{#with user.bestFriend as |friend|}}
  {{friend.nickname}}
{{/with}}

But things get hairy when these promises are passed to helpers and actions:

{{#with user.bestFriend as |friend|}}
  <button {{action "sendHi" friend}}>Send Hi!</button>
{{/with}}

...

actions : {
  sendHi(friend) {
    // friend.content
  }
}

So, I am very inclined to refactor my code using synchronous relationships. But what will I be missing?


#2

Well, there are obvious disadvantage with using sync relationship because it can’t handle relationship data that lives on another API endpoint.

Otherwise, there isn’t much difference from a template binding perspective.


#3

Well, yeah, but fetching data that lives on another API endpoint is usually done in the model hook of a route. And Ember Data is not big enough to support and ORM the way ActiveRecord does. So, altough I can do this:

model() {
  // var user ...
  // Fetch all friends informed by the relationships field from a response in JSON API?
  return user.get('friends');
}

I can’t do that:

model() {
  // var user...
  return user.get('friends').limit(20).where({ country : 'Japan' });
}

So, I end up using:

model() {
  return store.query('friend', { filter : { user_id : user.get('id'), country : 'Japan' }, limit : 20 });
}