我有一项名为clock
的服务//app/services/clock.js
export default Ember.Object.extend({
pulse: Ember.computed.oneWay('_seconds').readOnly(),
tick: function () {
var clock = this;
Ember.run.later(function () {
var seconds = clock.get('_seconds');
if (typeof seconds === 'number') {
clock.set('_seconds', seconds + (1/4));
}
}, 250);
}.observes('_seconds').on('init'),
_seconds: 0
});
我还有一个初始化器,我想注册时钟服务,然后将其注入我的“间隔”路由/控制器。
// app/initializers/clock-service.js
import ClockService from 'app/services/clock';
export default {
name: 'ClockServiceInitializer',
initialize: function(container, application) {
container.register('clock:service', ClockService);
app.inject('controller:interval', 'clock', 'clock:service');
}
};
编辑:似乎是这样:
export default {
name: 'services',
initialize: function(container, app) {
// Inject into all routes and controllers
// Or if you wanted, into a specific route
app.inject('controller:index', 'clock', 'service:clock');
}
};
答案 0 :(得分:2)
我有基本相同的东西,虽然我注入所有控制器。
在初始化程序中:
//container.typeInjection('component', 'store', 'store:main');
application.register('clock:service', ClockService, { singleton: true });
application.inject('controller', 'clock', 'clock:service');
在控制器中,使用它如:
updateView: {
// ...
}.observes('clock.pulse')