Best Practices: dynamically creating actions in Controller#init


#1

I have a controller that have a set of 3 actions repeated for 4 entities, I can’t extract this into a component.

Maybe that’s the ruby experience, but having 12 methods that do the same seems wrong, and I will need them on other controllers, so extracting to a mixin would help even more.

Here is the solution I used:

Suppose the functions are named showFirst, cancelFirst, saveFirst, etc.

export default Ember.ObjectController.extend({
  someEntities: ['first', 'second', 'third', 'fourth'],

  init: function() {
    var _this = this;
    this._super();

    this.get('entities').forEach(function(entity) {
      _this._actions[('show_' + entity').capitalize()] = function() {
        _this.foo(entity);
      };

      ...
  });
},
  ...
});

My concerns are:

  1. usage of the private API (_actions) – although I have unit tests for the generated functions.
  2. possible gotchas with bindings or something else

What is considering the best way to tackle this?