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

3

15

I want to use this.get('controllers.pack.query'); to get App.PackQueryController in App.PackController, but failed.

I think the problem is Ember use pack not pack.query as controllerName when it tries to get the controller. Although I can get the controller by this.controllerFor('pack.query'), but Ember says it is deprecated, please use needs instead

My router map likes below and I've defined needs: ['pack.query'] in App.PackController

App.Router.map(function () {
    this.resource('pack', function () {
        this.route('index', {path: '/:pack_id'})
        this.route('query');
    });
});

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

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

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

App.PackQueryController = Ember.ArrayController.extend({
    queryPack: function (queryKey) {
        //...do query pack
    }
});
Wyandotte answered 21/6, 2013 at 4:4 Comment(0)
D
16

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
   }
});
Decennary answered 21/6, 2013 at 5:24 Comment(1)
This does not work post ember-cli 0.2.3. See the answer by @Hulbard for a working solution.Crammer
H
35

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')
Hulbard answered 7/8, 2014 at 5:52 Comment(3)
Updated again to reflect new versions (Ember 1.11.1 and ember-cli 0.2.3)Hulbard
Thank you for this, there is surprisingly little information out there on this subject and getting to it seems to be a matter of stumbling blindly through the dark.Crammer
Updated again to reflect a newer version (Ember 1.13.5) that introduced a deprecation of the original answer.Hulbard
D
16

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
   }
});
Decennary answered 21/6, 2013 at 5:24 Comment(1)
This does not work post ember-cli 0.2.3. See the answer by @Hulbard for a working solution.Crammer
H
0

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

Haith answered 24/7, 2015 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.