Search code examples
ember.jsember-components

Avoid repetition of method name in actions


How do I avoid the following repetition of my foo method in my ember component?

Ember.Component.extend({
  ...

  foo(val) {
    this.set('baz', val);
  },

  actions: {
    bar() {
      this.foo(this.get('val'));

      // .. other code
    },
    foo(val) {
      this.foo(val);
    }
  }
});

Solution

  • Your code looks okay. If you really want to change something you could make foo method an action:

    Ember.Component.extend({
      ...
    
      actions: {
        bar() {
          this.send('foo', this.get('val'));
    
          // .. other code
        },
        foo(val) {
          this.set('baz', val);
        }
      }
    });