# Accessing another controllers model from a routeless controller

**URL:** <https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461>\
**Category:** Uncategorized\
**Created:** [October 9, 2014, 3:18pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461 "2014-10-09T15:18:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultimatemonty](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ultimatemonty/32/13849_2.png) [@ultimatemonty](https://discuss.emberjs.com/u/ultimatemonty)\
**Post date:** [October 9, 2014, 3:18pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/1 "2014-10-09T15:18:50Z")

</div>

In my app I have a list/detail view similar to many of the sample apps (list of posts on the left, selected post on the right). These are nested routes and work great. Inside the selected item template I have some buttons for creating a new item via a modal. I’m using the [Ember recommended approach](http://emberjs.com/guides/cookbook/user_interface_and_interaction/using_modal_dialogs/) for modals and the modals have a template and an associated controller, but no route. So far this is working great - modal loads, controller populates any required dynamic fields, clicking Submit/Cancel triggers the proper actions, everything is fine and dandy.

I’ve now run into an issue where I need to access another controller’s model from within my modal controller. Usually I’d use the `setupController` hook in the route to set what I need on the controller but since my modals are routeless this isn’t an option. I’ve tried using the `needs` API and accessing the model that way but `controllers.needed-controller.model` comes back as undefined/null. I’ve tried various ways to get at the needed model:

###From the modal controller

`parentModel: Ember.computed.alias('controllers.needed-controller.model')`,

```auto
parentModel: function() {
   return this.get('controllers.needed-controller.model');
}.property()

```

###From the modal template `{{controllers.needed-controller.model.property}}`

Am I missing something with the needs API? Am I batpooping crazy for using this pattern?

_ **EDIT** _

Working on a JSBin to illustrate the problem

---

<div class="post-metadata">

**Author:** ![ultimatemonty](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ultimatemonty/32/13849_2.png) [@ultimatemonty](https://discuss.emberjs.com/u/ultimatemonty)\
**Post date:** [October 9, 2014, 5:39pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/2 "2014-10-09T17:39:50Z")

</div>

Well I haven’t been able get a bin to reproduce a simplified case of what I’m trying to do which leads me to believe I’m doing something wrong in my code. Will continue to work at reproducing and post back if here if I’m successful at that or making it work.

---

<div class="post-metadata">

**Author:** ![ultimatemonty](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ultimatemonty/32/13849_2.png) [@ultimatemonty](https://discuss.emberjs.com/u/ultimatemonty)\
**Post date:** [October 9, 2014, 6:46pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/3 "2014-10-09T18:46:33Z")

</div>

OK I’ve somewhat traced this back to some namespacing weirdness.

If I type `App. __container__.lookup('controller:system-config.vendors.detail')` in the console I get the proper reference to the controller I need.

Along those same lines using `needs: system-config.vendors.detail` in my modal controller throws an error:

```auto
Uncaught Error: Assertion Failed: <App.VendorDivisionFormModalController:ember1819>#needs must not specify dependencies with periods in their names (system-config.vendors.detail)

```

If I use `needs: system-config-vendors-detail` I get a controller back but it’s empty.

So I guess the real question is **how to use NEEDS with a nested resource**

My router looks like:

```javascript
this.resource('system-config', function () {
   this.route('vendors', { path: 'vendors' }, function () {
      this.route('detail', { path: ':vendor_id' }); // <-- controller I need
   });
});

```

---

<div class="post-metadata">

**Author:** ![ultimatemonty](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ultimatemonty/32/13849_2.png) [@ultimatemonty](https://discuss.emberjs.com/u/ultimatemonty)\
**Post date:** [October 9, 2014, 7:33pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/4 "2014-10-09T19:33:42Z")

</div>

More troubleshooting with no solution yet:

`system-config/vendors/detail` errors out with `Uncaught Error: Assertion Failed: You are looking for a detail controller in the System-config.vendors namespace, but the namespace could not be found`

`system-config/vendors.detail` errors out with the `periods in their names` error

Looks like I just need to figure out how Ember wants me to reference that controller without the dots. I haven’t been able to find any solid guidance on deeply nested routes/controllers.

Would dependency injection be a viable alternative? Seems like a waste to have an initializer just to inject a single controller into another controller.

---

<div class="post-metadata">

**Author:** ![ultimatemonty](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ultimatemonty/32/13849_2.png) [@ultimatemonty](https://discuss.emberjs.com/u/ultimatemonty)\
**Post date:** [October 9, 2014, 7:43pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/5 "2014-10-09T19:43:22Z")

</div>

And the winner is… #`needs: 'system-configVendorsDetail'`#

[This set of tests](https://github.com/emberjs/ember.js/blob/4445be038bd839a1559020db5677b211dd3f1e9c/packages/ember-application/tests/system/dependency_injection/normalization_test.js#L18) in the Ember source was extremely helpful in figuring this out.

Seems a PR to the `needs` docs might be in my future…

Also - someone else please comment just so I don’t feel like I’m having a conversation with myself 🙂

---

<div class="post-metadata">

**Author:** ![broerse](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/broerse/32/9497_2.png) [@broerse](https://discuss.emberjs.com/u/broerse)\
**Post date:** [October 10, 2014, 5:52am UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/6 "2014-10-10T05:52:03Z")

</div>

😉 I have used needs to get

[https://github.com/broerse/ember-select-test/blob/master/app/controllers/page.js](https://github.com/broerse/ember-select-test/blob/master/app/controllers/page.js)

[https://github.com/broerse/ember-select-test/blob/master/app/templates/page-edit.hbs](https://github.com/broerse/ember-select-test/blob/master/app/templates/page-edit.hbs)

but the select in the pages edit route is only working if you click to the modelnames route first? I have put up a working version to show the problem.

[http://exmer.com/selecttest/](http://exmer.com/selecttest/)

If I write `{{mname.content.name}}` in

[https://github.com/broerse/ember-select-test/blob/master/app/templates/page.hbs](https://github.com/broerse/ember-select-test/blob/master/app/templates/page.hbs)

it get 1 modelname but the `contentBinding="controllers.modelnames"` it gets 0 modelname items.

Is this also a needs problem? Promise problem?

---

<div class="post-metadata">

**Author:** ![ultimatemonty](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ultimatemonty/32/13849_2.png) [@ultimatemonty](https://discuss.emberjs.com/u/ultimatemonty)\
**Post date:** [October 10, 2014, 1:47pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/7 "2014-10-10T13:47:29Z")

</div>

> [@broerse](#):
>
> but the select in the pages edit route is only working if you click to the modelnames route first? I have put up a working version to show the problem.

@broerse as I understand it controllers are singletons and not part of the application state. As such I don’t believe you can access any of the properties like `model` unless an active instance of the controller exists e.g. you’re in a route that uses that controller. So if any of your active routes don’t use the modelnames controller then you won’t be able to access any data in it. You can use [Ember Inspector](https://github.com/emberjs/ember-inspector) to view the current active routes/controllers via the **View Tree** tab:

I could be WAY off base here but that is my current understanding of how controllers work.

---

<div class="post-metadata">

**Author:** ![broerse](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/broerse/32/9497_2.png) [@broerse](https://discuss.emberjs.com/u/broerse)\
**Post date:** [October 10, 2014, 6:24pm UTC](https://discuss.emberjs.com/t/accessing-another-controllers-model-from-a-routeless-controller/6461/8 "2014-10-10T18:24:43Z")

</div>

Thanks. It works for {{mname.content.name}} from the pages route. It gets data from the modelnames route.

I thinks select should also do this out of the box.
