Skip to content

Feature Request: Allow composition of computed decorators #4

Open

Description

@pzuraq

For feature parity with ember-macro-helpers and ember-awesome-macros.

This works:

import EmberObject from 'ember-object';

import or from 'ember-awesome-macros/or';
import computed from 'ember-macro-helpers/computed';

export default EmberObject.extend({
  foo: computed(or('bar', 'qux'), function(barOrQux) {
  })
});

This, sadly, does not:

import EmberObject from 'ember-object';

import computed, { or } from 'ember-computed-decorators';

export default EmberObject.extend({
  @computed @or('bar', 'qux') // or @computed(or(..))
  foo(barOrQux) {
  }
});

This works (with kellyselden/ember-awesome-macros#348):

import EmberObject from 'ember-object';

import mapBy from 'ember-awesome-macros/array/map-by';
import sum from 'ember-awesome-macros/sum';
import raw from 'ember-macro-helpers/raw';

export default EmberObject.extend({
  items: [],
  
  totalQuantity: sum(mapBy('items', raw('quantity')))
});

This, sadly, does not:

import EmberObject from 'ember-object';

import { mapBy, sum } from 'ember-computed-decorators';

export default EmberObject.extend({
  items: [],

  @sum @mapBy('items', 'quantity') // or @sum(mapBy(..))
  totalQuantity
});

Activity

  1. pzuraq commented on Feb 23, 2018

    @pzuraq
    CollaboratorAuthor

    I feel like this should probably be upstreamed to Ember actually, but I think we can support it in the syntax for this macro

  2. lolmaus commented on Dec 27, 2018

    @lolmaus

    Any ideas? Not being able to compose macros is a major setback for me.

    CC @wycats @rwjblue

  3. pzuraq commented on Dec 28, 2018

    @pzuraq
    CollaboratorAuthor

    It actually turned out that composable macros were upstreamed to Ember quite a while ago, and then reverted. I believe this was in the v1 era, @twokul was the person who worked on it so he may have more context.

    I think based on that, this will probably make sense more as an external library. I also think that based on the direction of @tracked, it would make sense as a non-Ember-specific library of macro decorators that don't have caching, and would actually be much simpler. So you would basically be able to do:

    import { tracked } from '@glimmer/tracking';
    
    import { mapBy, sum } from 'awesome-decorator-macros';
    
    class Foo {
      @tracked 
      @sum(mapBy('items', 'quantity'))
      totalQuantity;
    }

    Note that I'm using the functional/nested syntax now (@sum(mapBy(...)) instead of @sum @mapBy(...)) because I think it'll scale better as more decorators become available, otherwise it becomes confusing which decorators relate and which do not, etc.

  4. lolmaus commented on Dec 30, 2018

    @lolmaus

    This sounds like a great idea, @pzuraq! 👍

    I'm only starting to change my mindset towards decorators. Looks like there are many possibilities I'm missing. ^_^

  5. lolmaus commented on Jan 25, 2019

    @lolmaus

    This works today:

    import { wrapComputed as cp } from '@ember-decorators/object';
    import { findBy } from 'ember-awesome-macros/array';
    import raw from 'ember-macro-helpers/raw';
    
    class Foo {
      people: Person[];
    
      @cp(findBy('people', raw('name'), raw('Michael')))
      michael?: Person;
    }

    @pzuraq Thank you for the hint!

  6. lolmaus commented on Jun 5, 2019

    @lolmaus

    With every CP dubbing as a decorator, this is now possible:

    import { findBy } from 'ember-awesome-macros/array';
    import raw from 'ember-macro-helpers/raw';
    
    class Foo {
      people: Person[];
    
      @findBy('people', raw('name'), raw('Michael'))
      michael?: Person;
    }

    It feels SO FREAKING GOOD! Big thanks to @pzuraq and everyone who made it possible! 🙇 🙇 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions