I have an initializer that subscribes to Pusher events that tell when a model is updated on the server, and updates it on the client correspondingly. Here I have it set up to update the player
and team
models:
export default {
name: 'pusher-updates',
after: 'store',
initialize: function(container) {
var store = container.lookup('store:main');
var pusherConnection = new window.Pusher(AppENV.pusherKey);
var mainChannel = pusherConnection.subscribe('main-updates');
mainChannel.bind('update-player', function(data) {
store.update('player', data.player);
});
mainChannel.bind('update-team', function(date) {
store.update('team', data.team);
});
}
};
This all worked very fine, but then I needed to update the current user. For my other models, I want to broadcast model changes to every client, but for user model updates, I only want to broadcast updates to the client for that particular user.
The current user is set up in an initializer of its own:
import Ember from 'ember';
import Session from 'simple-auth/session';
var SessionWithCurrentUser = Session.extend({
currentUser: function() {
var userId = this.get('id');
if (!Ember.isEmpty(userId)) {
var store = this.container.lookup('store:main');
return store.find('user', userId);
}
}.property('id')
});
export default {
name: 'customize-session',
initialize: function(container) {
container.register('session:withCurrentUser', SessionWithCurrentUser);
}
};
But now, the customize-session
initializer needs access to pusherConnection
. My initial thought was to create another initializer that sets up the Pusher connection and injects it into the application. But where would it inject it to, when it needs to be accessed by other initializers? The Ember docs say āInjections can be made onto all of Emberās major framework classes, including views, helpers, components, controllers, routes, and the router.ā Is there a way to inject into initializers?
For now, I hacked it and made pusherConnection
a property on window
so that I could access it from the second initializer, like this:
var SessionWithCurrentUser = Session.extend({
currentUser: function() {
var userId = this.get('id');
if (!Ember.isEmpty(userId)) {
var store = this.container.lookup('store:main');
var user = store.find('user', userId);
var userChannel = window.pusherConnection.subscribe('user-' + userId);
userChannel.bind('update-user', function(data) {
store.update('user', data.user);
});
return user;
}
}.property('id')
});
But that just feels dirty. Whatās the right way to do this?