Ember.js How to get controller in needs which is nested controllerName

烂漫一生 提交于 2019-11-30 17:27:37

You should use camel case, not dot notation for this.

Your pack controller should be

 App.PackController = Ember.ObjectController.extend({
   needs: ['packQuery'],
   queryPack: function () {
     var packQueryCtrller = this.get('controllers.packQuery');            

     Ember.debug('packQueryCtrller: ' + packQueryCtrller);
     //DEBUG: packQueryCtrller: undefined

     packQueryCtrller.queryPack(); //faild packQuery is undefined
   }
});

Ember.inject.controller() should be used to access a controller. Use it like so:

Setting

...
myController: Ember.inject.controller('pack'),
nestedController: Ember.inject.controller('pack/query')
...

Getting

...
this.get('myController');
this.get('nestedController');
...

The answer above was updated to reflect the needs deprecation in Ember 1.13.5 (released July 19, 2015). I've left the old answers below, but shouldn't be used unless you're using an older version of Ember.


[DEPRECATED] Accessing nested controllers from other controllers using needs:

Set needs on the controller:

...
needs: ['pack/query'],
...

Then access it using:

this.get('controllers.pack/query');

[DEPRECATED] Accessing nested controllers from routes:

Ideally, actions should be put on a Route. If you're using the needs pattern outlined above in your actions on a controller, consider refactoring.

You can access nested controllers from a Route using controllerFor like so:

this.controllerFor('pack/query')

There's a newer, inject, syntax for same use case

accountQueueController: Ember.inject.controller('account/queue'),
...
this.get('accountQueueController.model.myProperty')

source: http://discuss.emberjs.com/t/needs-with-nested-controller/8083/6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!