Explore new patterns of event-driven communication with
ember-message-bus. This addon adds a simple service to your application,
message-bus, which you can use to facilitate loosely coupled communications between services, components, and other Ember objects.
ember install ember-message-bus
First, inject the
message-bus service into your object:
message-bus has been injected, you can
subscribe to events through it:
subscribe expects three arguments: 1) the name of the event, 2) the context, typically
this, and 3) the callback.
Finally, you can
publish events to trigger the subscription:
publish expects one or more arguments. The first argument must be the name of the event. After that, you can pass as many arguments as you like into
publish. These arguments will be handed to the subscribing callback.
It's easy to test if a message is published. First, run
If you'd like to ensure that events are published, use the Qunit assertion
willPublish like so:
Note that if you want to test that args are published, the expected args should be passed in as an array. Alternatively, you can provide a callback that will receive the args as their params.
On the other end, if you want to confirm that a message was not published, you can use
Note that in both cases, these assertions should be made before the message is actually published. So in a component integration test:
const foo = bar: 'baz' ;this;assert;this;