Ember.js 如何获取嵌套控制器名称的控制器 [英] Ember.js How to get controller in needs which is nested controllerName

查看:24
本文介绍了Ember.js 如何获取嵌套控制器名称的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 this.get('controllers.pack.query'); 来获取App.PackQueryControllerApp.PackController 中,但是失败了.

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

我认为问题是 Ember 使用 pack 而不是 pack.query 作为 controllerName当它尝试获取控制器时.虽然我可以通过 this.controllerFor('pack.query') 获取控制器,但是 Ember 说它已被弃用,请改用 needs

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

我的路由器映射如下所示,我在 App.PackController

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

推荐答案

你应该使用驼峰式大小写,而不是点符号.

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

你的包控制器应该是

 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.js 如何获取嵌套控制器名称的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆