Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service wont start unless I console log clock #7

Open
jrock2004 opened this issue May 20, 2016 · 4 comments
Open

Service wont start unless I console log clock #7

jrock2004 opened this issue May 20, 2016 · 4 comments

Comments

@jrock2004
Copy link

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?

@jerel
Copy link
Owner

jerel commented May 23, 2016

Interesting. Does adding clock (instead of clock.time) to Ember.computed have any effect?

@willemdewit
Copy link

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.

@SirZach
Copy link

SirZach commented Dec 5, 2016

manually calling start in the init handler of my own controllers/components worked as well

@ryanto
Copy link

ryanto commented May 22, 2019

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);
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants