sturoid
sturoid

Reputation: 2711

Resolving promises on filter

I have a filter and I can't get it to resolve the promise. It is using a model called 'patient' that hasMany 'addresses' and I want to filter out the address that has an addressType of 'Primary'. The filter seems to be working but only will return a promise. Thank you in advance for the help.

Patient controller

App.PatientController = Ember.ObjectController.extend

  primaryAddress: Em.computed '[email protected]', ->
    @get('model.addresses').then (addresses)->
      addresses.filterBy 'addressType', 'Primary'

enter image description here

Solution (Thanks to @GJK)

  primaryAddress: Em.computed 'model.addresses.@each', ->
    @get('model.addresses').filterBy('addressType', 'Primary').get('firstObject')

Upvotes: 0

Views: 80

Answers (1)

GJK
GJK

Reputation: 37369

The most obvious issue that I can see is that you're not watching the @each property of the addresses. Your property depends on [email protected], which means that your model is an array, and you're observing the addresses property on each item in that array. But you're not observing the content of the addresses array, just the array itself.

It seems to me that you should use model.addresses.@each as your dependent property. This will observe all addresses on a single model, including observing the contents of the addresses array (which updates when the promise resolves).

EDIT: Also, I didn't quite read far enough apparently. You shouldn't be calling then on the promise. Treat it as if it's already resolved, and it'll be updated when it does resolve. So use this instead:

App.PatientController = Ember.ObjectController.extend
  primaryAddress: Em.computed 'model.addresses.@each', ->
    @get('model.addresses').filterBy 'addressType', 'Primary'

The first time this property comptues, the promise will be unresolved so the filter won't return anything. But when the promise resolves, the property will update and the filter will work like you expect it to.

Upvotes: 1

Related Questions