Accessing another controllers model from a routeless controller


#1

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 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'),

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


#2

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.


#3

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:

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:

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

#4

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.


#5

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

This set of tests 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 :slight_smile:


#6

:wink: I have used needs to get

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/

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

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

Is this also a needs problem? Promise problem?


#7

@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 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.


#8

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.