Let’s say you have a few or several computed properties, some of which are kinda complex, that you want to share between 2 (or more) controllers. In the interest of keeping things DRY, what would be the best way to do this?
I tried to do it as a mixin, but couldn’t get export
to work, if for no other reason than the computed properties are separated by commas, not semicolons.
So the only thing I can think of is to define the comp. props. in a parent controller and then derive the using controllers from it.
But to me that kinda sorta smells because I have to name it something like MyParentController
and stick it in the top level of the /controllers
directory. (Although I could create a separate directory for it, I guess.) And all it does is define a few/several computed properties.
The comp. props.behave like functions => seems they should be able to be mixed in to the controllers that need them, as opposed to making those controllers inherit from a base class that just defines them.
BTW, I realize that comp. props. are normally relative to (i.e. use) properties within the same class. In this case my 2 child controllers are very similar (deal with same object: one is for list-all and the other is for show-one), while the properties that they care about (i.e. need in their computations) live in another controller.
So maybe this arrangement can only result in either code duplication or odor.
Okay, as I think about this even more… this doesn’t have to be specifically about sharing computed properties - it’s probably not uncommon for controllers to share all kinds of stuff. Heck, look at Rails: they have a base ApplicationController
that you derive your own controllers from, and it’s not uncommon to create other controllers that derive from ApplicationController
, add some functionality, and then derive from them.
It’s just that I’ve never run across controller derivation in my Ember readings, hence my ignorance on how people address/solve this problem.