Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber.js dependency injection
    text
    copied!<p>let's assume I have this controller</p> <pre><code>MyApp.LayoutFooterController = Ember.ObjectController.extend formData: name: null, phone: null, message: null cleanFormData: -&gt; @set('formData.name', null) @set('formData.phone', null) @set('formData.message', null) send: () -&gt; @container.lookup('api:contact').send( @get('formData.name'), @get('formData.phone'), @get('formData.message') ) @cleanFormData() </code></pre> <p>For this I've created service class</p> <pre><code>MyApp.Api ||= {} MyApp.Api.Contact = Ember.Object.extend init(@$, @anotherDep) -&gt; send: (name, phone, message) -&gt; console.log name, phone, message </code></pre> <p>and initializer</p> <pre><code>Ember.Application.initializer name: 'contact' initialize: (container, application) -&gt; container.register 'api:contact', MyApp.Api.Contact </code></pre> <p>Problem is, that I can not figure out how to set container to be able to resolve my service class dependecies <code>init(@$, @anotherDep)</code> through Ember container.</p> <p>Can anybody give me explanation, how to use the Ember.js dependecy injection (or service locator, I guess) container to inject other libs or objects? </p> <p>Maybe, that I'm not doing it well at all.</p> <p><strong>EDIT</strong></p> <p>When I looked to Ember's <a href="https://github.com/emberjs/ember.js/blob/master/packages/container/lib/main.js#L259" rel="nofollow">container source code</a> I found a solution:</p> <pre><code>Ember.Application.initializer name: 'contact' initialize: (container, application) -&gt; container.register 'api:contact', { create: () -&gt; new MyApp.Api.Contact(application.$) } </code></pre> <p>But is this clean?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload