While Ember's routes and components are highly event-driven, this pattern does not extend to services, which must be tightly coupled to the components and other services that interface with them. With ember-message-bus
, we can loosen that coupling by allowing those interactions to also be event-driven.
ember install ember-message-bus
First, add the BusPublisherMixin
to something (such as a component, route, or service):
import Ember from 'ember';
import { BusPublisherMixin } from 'ember-message-bus';
export default Ember.Service.extend(BusPublisherMixin, {
});
Next, send messages to the message bus with publish
:
export default Ember.Service.extend(BusPublisherMixin, {
init() {
this.publish('serviceBooted', this);
this._super();
}
});
Finally, add the BusSubscriberMixin
to anything that you want to listen for these messages:
import Ember from 'ember';
import { BusSubscriberMixin } from 'ember-message-bus';
export default Ember.Service.extend(BusSubscriberMixin, {
services: Ember.computed(() => Ember.A()),
addService: on('serviceBooted', function(service) {
this.get('services').pushObject(service);
})
});