lundi 29 octobre 2018

How to use a thenable with a computer property in Ember?

What is the correct way to use a thenable as one of the dependent keys of a computed property in Ember in 2018?

I would like the computed property to update when the promise/thenable resolves without jumping through hoops. I do this:

payableId: computed('paymentRecord.isSettled', function() {
  let paymentRecord = this.get('paymentRecord');
  if (paymentRecord.get('isSettled') === false) return;
  if (paymentRecord.content) paymentRecord = paymentRecord.content;

  return paymentRecord.belongsTo('payable').id();
}),

In my example, I have a paymentRecord which belongs to a payable and I want to get some information about the payable but the paymentRecord is not resolved until some unknown point in the future. The paymentRecord my or may not be a thenable so we have to jump through various hoops:

  • Have to be careful to check isSettled against false since undefined would indicate not a thenable
  • Have to drill down into .content if it exists in the case of a thenable

But it feels like a hack and I can't find anything about this in the guides.




Aucun commentaire:

Enregistrer un commentaire