简体   繁体   English

从Route内的Ember setupController调用控制器函数

[英]Calling controller function from Ember setupController inside Route

How can I call a function inside action handlebars from setupController located in the Route. 如何从Route中的setupController的动作手把内调用函数。 (Also, how can I call a function inside a controller and not the route itself). (此外,如何在控制器内部而不是路由本身调用函数)。

I want my page to analyse the URL parameters and fill in some variables based on those parameters. 我希望我的页面分析URL参数并基于这些参数填写一些变量。

App.CresRoute = Ember.Route.extend({
  setupController: function(){
    this.send('testFunction', "Print me");
  },

  actions: {
    testFunction: function(string){
        console.log(string);
    },
  }
});

This returns an error: "Nothing handled the function 'testFunction'." 这将返回错误:“没有任何处理函数'testFunction'。”

Obviously my methods are more complex, with many parameters, and this is just to demonstrate the problem. 显然,我的方法更复杂,有很多参数,这只是为了演示问题。

I've edited this to try to clarify your question, but I'm not sure what you mean by "call a function inside action handlebars from setupController". 我已经对此进行了编辑,以试图阐明您的问题,但是我不确定“从setupController调用动作把手内部的函数”的含义。

In the sample code you've included, for testFunction to respond, it would need to be higher up the route tree from where it's called. 在您包含的示例代码中,为了使testFunction响应,它需要在调用它的路由树上更高。 Actions go up. 行动起来。 If you're trying to set variables on the controller, you're right to be doing it in the setupController hook, but you don't need actions to do it; 如果您想在控制器上设置变量,则可以在setupController挂钩中进行设置,但是您无需执行任何操作; just define testFunction outside the actions block, and call it like a normal method on your route: 只需在action块外定义testFunction ,然后像在路由上的普通方法一样调用它即可:

App.CresRoute = Ember.Route.extend({
  setupController: function(){
    this.testFunction("Print me");
  },

  testFunction: function(string){
    console.log(string);
  }
});

I don't know what Handlebars has to do with this question, so I wonder if I've understood properly what you're trying to do? 我不知道Handlebars与这个问题有什么关系,所以我想知道我是否已经正确理解了您要做什么?

This should work for you. 这应该为您工作。

App.CresRoute = Ember.Route.extend({
  setupController: function(){
    this.actions.testFunction('Print me');
  },

  actions: {
    testFunction(string){
        console.log(string);
    },
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM