ember.jsember-dataember-octane

Find-by with multiple attribute in Ember.js Octane


I have a route with RSVP that load several models (categories with items, treatments, prices) which return in JsON API Forrmat and want to show them in a table:

Item Name Treatment Name 1 Treatment Name N
Category 1
Item 1 in Cat 1 Price item 1 in Cat 1 for Treatment 1 Setup Price
Item 2 in Cat 1 Setup Price Price item 2 in Cat 1 for Treatment N
Category 2
Item 1 in Cat 2 Price item 1 in Cat 2 for Treatment 1 Price item 1 in Cat 2 for Treatment N

Setup Price only show if there isn't any price found, so the user can setup the price for the item in that treatment

I already can iterate the categories, items and treatment; but still do not know how to configure to show the correct price.

How can I find the correct prices by 2 attributes? I tried using find-by helper in the template:

{{#let (find-by "item.id" item.id @model.prices) as |price|}}
  {{price.amount}}
{{/let}}

, but it seems it only support 1 attributes.

Well, if in SQL it would be something like:

select * from prices where item.id = [currentItemId] and treatment.id = [currentTreatmentId]

but I want it to search in the already loaded price model...

Any help ? thank you...


Solution

  • If you're using find-by from ember-composable-helpers it looks like you have the correct syntax. You may need to update this addon if it is installed or else investigate where find-by is coming from in your app as it isn't a default ember helpers. You can also solve this with a helper. Invoked as:

    {{#let (find-price-for-item item @model.prices) as |price|}}
      {{price.amount}}
    {{/let}}
    

    Which could look something like:

    // app/helpers/find-price-for-item.js
    import { helper } from '@ember/component/helper';
    
    function findPriceForItem([item, prices]) {
      return prices.find(price => price.item.id === item.id);
    }
    
    export default helper(findPriceForItem);