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

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

问题描述

我想使用 this.get('controllers.pack.query'); 来获取
App.PackQueryController App.PackController ,但失败。



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



我的路由器映射喜欢下面,我定义了 needs: $



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

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

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

packQueryCtrller.queryPack(); // faild packQuery未定义
}
});

App.PackQueryController = Ember.ArrayController.extend({
queryPack:function(queryKey){
//...do查询包
}
});


解决方案

您应该使用驼峰盒,而不是圆点符号



您的包控制器应为

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

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

packQueryCtrller.queryPack(); // faild packQuery未定义
}
});


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

解决方案

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

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