# Dynamic computes

**URL:** <https://discuss.emberjs.com/t/dynamic-computes/15606>\
**Category:** General\
**Created:** [October 7, 2018, 9:53pm UTC](https://discuss.emberjs.com/t/dynamic-computes/15606 "2018-10-07T21:53:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahopkins](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahopkins/32/13879_2.png) [@ahopkins](https://discuss.emberjs.com/u/ahopkins)\
**Post date:** [October 7, 2018, 9:53pm UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/1 "2018-10-07T21:53:06Z")

</div>

Is there such as thing as dynamic `computed` properties? I think a bit of code would make more sense.

Let’s say there is an object like this:

```auto
const fruitVendor = {
    weeks: {
        '2018-10-07': {
            available: ['apples', 'bananas', 'pears']
        },
        ...
    },
    ...
}

```

The data will always be in this format: `fruitVendor.weeks.<YYYY-MM-DD>.available`.

There could be a lot `weeks`, but I may only care about a computed property on this week. Right now I need to do something like this:

```auto
myComputedProperty: computed('fruitVendor.weeks', ...)

```

Whereas, ideally, I had something like this:

```auto
myComputedProperty: computed('fruitVendor.weeks.<getThisWeek()>.available', ...)

```

How do you handle something like this?

---

<div class="post-metadata">

**Author:** ![NullVoxPopuli](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nullvoxpopuli/32/18691_2.png) [@NullVoxPopuli](https://discuss.emberjs.com/u/NullVoxPopuli)\
**Post date:** [October 7, 2018, 10:25pm UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/2 "2018-10-07T22:25:37Z")

</div>

You could use another computed with a second dependent key on the week of interest?

```nohighlight
@computed('weeks', 'selectedWeek')
get someData() {
  return this.weeks[this.selectedWeek];
}

```

---

<div class="post-metadata">

**Author:** ![ahopkins](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahopkins/32/13879_2.png) [@ahopkins](https://discuss.emberjs.com/u/ahopkins)\
**Post date:** [October 7, 2018, 10:31pm UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/3 "2018-10-07T22:31:29Z")

</div>

Thanks. I will try this.

---

<div class="post-metadata">

**Author:** ![ef4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ef4/32/13470_2.png) [@ef4](https://discuss.emberjs.com/u/ef4)\
**Post date:** [October 8, 2018, 12:05pm UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/4 "2018-10-08T12:05:14Z")

</div>

Sometimes in these cases, things get simpler if you replace some of your computed properties with helpers.

---

<div class="post-metadata">

**Author:** ![alexspeller](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alexspeller/32/14812_2.png) [@alexspeller](https://discuss.emberjs.com/u/alexspeller)\
**Post date:** [October 9, 2018, 10:57am UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/5 "2018-10-09T10:57:09Z")

</div>

This is quite easy to do in plain ember once you get your head around it:

> **[Ember Computed Properties with Computed Dependent Keys](https://spin.atomicobject.com/2015/08/03/ember-computed-properties/)**
>
> Although not built into Ember, there is a straightforward approach to defining computed properties that have dynamic, or computed, dependent keys.

There’s also an addon but I haven’t tried it:

> **[GitHub - gordonkristan/ember-computed-indirect](https://github.com/gordonkristan/ember-computed-indirect)**
>
> Contribute to gordonkristan/ember-computed-indirect development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![ahopkins](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahopkins/32/13879_2.png) [@ahopkins](https://discuss.emberjs.com/u/ahopkins)\
**Post date:** [October 17, 2018, 10:31am UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/6 "2018-10-17T10:31:32Z")

</div>

Thanks @alexspeller. While I didn’t do it exactly like in the linked article, I used `defineProperty` in `init` so it is something like this:

```auto
    init() {
        this._super(...arguments);
        const currentWeek = this.get('week');
        defineProperty(this, '_availableCurrentWeek', computed.oneWay(`fruitVendor.weeks.${currentWeek}.available`));
    },

```

---

<div class="post-metadata">

**Author:** ![CezaryH](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/cezaryh/32/13527_2.png) [@CezaryH](https://discuss.emberjs.com/u/CezaryH)\
**Post date:** [December 5, 2018, 4:31pm UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/7 "2018-12-05T16:31:01Z")

</div>

I would suggest not using “defineProperty”.

In docs you can find sentence:

“NOTE: This is a low-level method used by other parts of the API. You almost never want to call this method directly.”

> **[defineProperty - 3.5 - Ember API Documentation](https://api.emberjs.com/ember/3.5/functions/@ember%2Fobject/defineProperty)**
>
> NOTE: This is a low-level method used by other parts of the API. You almost never want to call this method directly. Instead you should use mixin() to define new properties. Defines a property on an object. This method works much like the ES5...

But you cen easily set property

```auto
init() {
        this._super(...arguments);
        const currentWeek = this.get('week');
        set(this, '_availableCurrentWeek', computed.oneWay(`fruitVendor.weeks.${currentWeek}.available`));
    },

```

---

<div class="post-metadata">

**Author:** ![sukima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/sukima/32/5773_2.png) [@sukima](https://discuss.emberjs.com/u/sukima)\
**Post date:** [July 5, 2019, 2:16am UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/8 "2019-07-05T02:16:42Z")

</div>

The use of either `defineProperty` or `set` a `computed` was removed. This example no longer works.

---

<div class="post-metadata">

**Author:** ![sukima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/sukima/32/5773_2.png) [@sukima](https://discuss.emberjs.com/u/sukima)\
**Post date:** [July 5, 2019, 2:39am UTC](https://discuss.emberjs.com/t/dynamic-computes/15606/9 "2019-07-05T02:39:11Z")

</div>

Since I already resurrected this thread I should at least followup with a nifty solution to _my_ dilemma regarding dynamic dependencies. I was inspired by [the link](https://spin.atomicobject.com/2015/08/03/ember-computed-properties/) given by @alexspeller above.

I had a need to have a POJO passed into a component and then have the component glean dependencies from the object’s keys.

```nohighlight
  state: reads('stateManager.state'),
  isAllSelected: equal('state', STATES.ALL),
  isNoneSelected: equal('state', STATES.NONE),
  isSomeSelected: equal('state', STATES.SOME),

  stateManager: computed('selected', function() {
    let properties = Object.keys(this.selected);
    return EmberObject.extend({
      properties,
      state: computed(`selected.{${properties.join(',')}}`, function() {
        let selections = Object.values(this.selected);
        if (selections.every(x => x)) {
          return STATES.ALL;
        } else if (selections.every(x => !x)) {
          return STATES.NONE;
        } else {
          return STATES.SOME;
        }
      })
    }).create({ selected: this.selected });
  }),

```

The caveat is that this will not detect new keys or deleted keys but in my case this was fine as the `selected` POJO would never change like that. I needed only to track the values of those keys but the keys were not known until the component was initialized. This works for that use case.
