ember-message-bus
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.
Installation
ember install ember-message-bus
Usage
First, inject the message-bus
service into your object:
; Component;
Once the message-bus
has been injected, you can subscribe
to events through it:
; Component;
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:
; Component;
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.
Testing
It's easy to test if a message is published. First, run initializeQUnitAssertions
:
; ;
If you'd like to ensure that events are published, use the Qunit assertion willPublish
like so:
assert;assert;assert;
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 willNotPublish
:
assert;
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;