# Using needs to inject objects into other objects

**URL:** <https://discuss.emberjs.com/t/using-needs-to-inject-objects-into-other-objects/6810>\
**Category:** Uncategorized\
**Created:** [November 29, 2014, 5:09am UTC](https://discuss.emberjs.com/t/using-needs-to-inject-objects-into-other-objects/6810 "2014-11-29T05:09:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![codematix](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/codematix/32/9720_2.png) [@codematix](https://discuss.emberjs.com/u/codematix)\
**Post date:** [November 29, 2014, 5:09am UTC](https://discuss.emberjs.com/t/using-needs-to-inject-objects-into-other-objects/6810/1 "2014-11-29T05:09:20Z")

</div>

I wanted to understand if the `needs` attribute could be used to inject arbitrary objects into controllers, routes and views.

I am developing an Ember.js application where I am writing a custom data-service layer that communicates with the backend to load and persist data. I define Ember Objects which represent the various backend services such as:

```auto
App.SessionServiceClient = Em.Object.extend({
  // methods and attributes
});

App.UserServiceClient = Em.Object.extend({
  // methods and attributes
});

```

I now register these objects with the application’s container to make them available for DI:

```auto
App.register('service:session', App.SessionServiceClient, {singleton: false});
App.register('service:user', App.UserServiceClient, {singleton: false});

```

Now that the objects are available for injection, and if I have a controller that needs only the SessionServiceClient, can I do the following:

```auto
App.SignInController = Em.ObjectController.extend({
  needs: ['service:user'], // using the needs to declare dependency
  actions: {
    // actions for the view
  }
});

```

When i tried this, it did not work. Is this possible with Ember.js or am I doing it wrong?

---

<div class="post-metadata">

**Author:** ![eoinkelly](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eoinkelly/32/15679_2.png) [@eoinkelly](https://discuss.emberjs.com/u/eoinkelly)\
**Post date:** [November 30, 2014, 8:08am UTC](https://discuss.emberjs.com/t/using-needs-to-inject-objects-into-other-objects/6810/2 "2014-11-30T08:08:07Z")

</div>

`inject` is your friend - see [http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/#toc\_dependency-injection-with-code-register-inject-code](http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/#toc_dependency-injection-with-code-register-inject-code) - you can inject your registered service into all controllers or an individual controller in an initializer (see link for details). AFAIK `needs` only works with other controllers.

HTH

/Eoin/
