An Ember service for managing a Horizon connection, and an adapter to request data from that connection.
ember install ember-cli-horizon
config/environment.js
var ENV = {
// ...
horizon: {
host: 'localhost:8181'
// Any other config options: http://horizon.io/api/horizon/#constructor
}
// ...
};
app/foo/component.js
import Ember from 'ember';
import { HorizonConnectionService } from 'ember-cli-horizon/services/horizon-connection';
export default Ember.Component.extend({
horizonConnection: Ember.inject.service(),
init() {
this._super(...arguments);
const horizonConnection = this.get('horizonConnection');
if (horizonConnection.isReady) {
someMethodThatUsesHorizon(horizonConnection.hz);
} else {
horizonConnection.connect().then(hz => {
if (this.isDestroying || this.isDestroyed) {
return;
}
someMethodThatUsesHorizon(hz);
}); // Should also catch a connection error here and handle it
}
}
})
app/foo/adapter.js
export { default } from 'ember-cli-horizon/adapters/horizon';
Now just use the foo model like any other. No other work required. Note that this naively maps EmberData model name to Horizon collection name. I haven't figured out a good way to alter this mapping without making any silly couplings that I'd like to avoid, let me know if you have any ideas.