Need help on how to "tell" a component to do something


#1

I’ve got a component which has a “layout” routine where it calculates its size and lays out its contents accordingly. I’m using the ResizeAware addon to run that layout logic when the browser resizes. However, I also want to run that layout logic when a certain event occurs. So if my route knows about this event, how does it communicate with the component to tell it to run the ‘layout’ method again? Do I need a controller with a bound property that the component watches and calls ‘layout’ when that property changes? (I was thinking that controllers are going away so this seems odd)


#2

You could use a service to communicate the layout event from the router to anywhere in the app including components.


#3

Thanks for the reply. So you are saying the service could act as a messaging bus and I would pass messages/events with an identifier that the component could listen for and act upon (assuming the identifier matched that component, thus allowing me to use the service for multiple components)? I think I can do that; would that be a “sanctioned ember-way” of solving this issue? Sounds like it would work but I’ve not read about other doing that so I wasn’t sure if that was a common use for services.

Thanks


#4

@billdwhite I often have this use case in my app. Using the “message bus” pattern seems to come under fire sometimes as not the “emberist way” to do it, but yet sometimes it’s needed. Here is a twiddle I built a while back to demonstrate this pattern: http://ember-twiddle.com/b062f2821381526446dc


#5

I think this is the closest there is to an ember sanctified answer that I’ve found How to communicate to child components


#6

Thanks for the replies; I was able to make this work using the service you described :wink: