Ember.js How to get controller in needs which is nested controllerName-Record and share programming errors

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
    }
});
  1. 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
       }
    });
    

Originally posted 2013-09-28 06:10:24.