samedi 11 novembre 2017

How can I pass arguments to an ember service while it is injected?

I have a component, say MyComponent. I have injected a service into this component, say MyService.

When 'MyComponent' is used inside another component(say ParentComponent), it passes some arguments to 'MyComponent' through the template. 'MyService' which is injected in 'MyComponent' needs this template arguments. How can I pass it to the service while injecting the service?

Right now I have written a special function inside the service which will be called in the init() function of 'MyComponent'. I don't know if that is the right way.

Here is a rough code example:

parent-component/template.hbs:

// some html

// some html

my-component/component.js:

export default Component.extend({
 // some js
 myService: inject.service(),
 init() {
  this._super(...arguments);
  get(this, 'myService').passDownMyArguments(get(this, 'myOptions'));
 },
 // some js again
})

I want to know if I can pass 'myOptions' to the service when it is injected rather than calling another function within the service.




Aucun commentaire:

Enregistrer un commentaire