Best way to share computed properties?


#1

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.


#2

A mixin should work but the mixin should export a mixin object (aka an object of type mixin from Ember.Mixin.create) which has those computed properties, and then each controller mixes in the mixin when extending the controller. Alternatively you could put each CP in a separate mixin and mix in only the ones that you want.

You could also look into CP macros to see if that would help in your situation. I think ember awesome macros is one such addon and dockyard had some blog posts and/or addons for that stuff too but can’t remember right offhand.


#3

@dknutsen: Thanks for the reply. I’ve noticed that you answer a lot of questions for people (you’ve helped me out before) - and I’m sure I speak for all of us when I say: Thank you! Thank you! Thank you!


#4

BTW, your suggestion worked. My problem was that I was trying to export each individual computed property, as opposed to just exporting a mixin object.

In case anyone else finds this interesting, the only hiccup I found in your suggestion was from the Mixin doc: Note that Mixins are created with Mixin.create , not Mixin.extend


#5

Glad if I can help! Always love to see people picking up Ember. I’ve also learned a lot from answering or trying (in many cases) to answer questions.


#6

oh good call I’m sloppy with the difference sometimes, will edit my original comment!


#7

Be sloppy all you want, man. Just keep those cards and letters - and answers - coming in. :grinning: