ember-cli-clock
ember-cli-clock copied to clipboard
Service wont start unless I console log clock
So I am trying to inject the clock service in my model. But it will not start unless I interact with the clock service.
clock: Ember.inject.service('booking-counter'),
isAvailable: Ember.computed('deadline', 'isbookable', 'clock.time', 'clock', function() {
console.log(this.get('clock'));
if(!this.get('isbookable')) {
return false;
}
return moment(this.get('deadline')) > moment();
})
If I take out the console log the service will not start. Any ideas on what I am doing wrong?
Interesting. Does adding clock
(instead of clock.time
) to Ember.computed have any effect?
I have the same problem, adding a binding to clock
does not work. The init
method on the service is not invoked, so it is not started.
manually calling start
in the init
handler of my own controllers/components worked as well
I just ran into this and learned a bit about how service injection works. Hopefully this helps anyone else running into this issue...
Services are lazily injected into Ember objects, so clock
will be null
until something tries to access/get it.
One way to make sure your clock service gets injected is to make your computed property accesses it.
So, instead of writing...
isAvailable: computed('availableDate', 'clock.date', function() {
return moment(this.availableDate).isAfter(new Date());
})
you should write something the accesses the clock, forcing injection to take place. For example:
isAvailable: computed('availableDate', 'clock.date', function() {
- return moment(this.availableDate).isAfter(new Date());
+ return moment(this.availableDate).isAfter(this.clock.date);
})