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

Calling controller function from Ember setupController inside Route

我如何从位于 Route 中的 setupController 调用操作手柄内的函数。 (另外,我如何调用控制器内部的函数而不是路由本身)。

我希望我的页面分析 URL 参数并根据这些参数填充一些变量。

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

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

这returns一个错误:"Nothing handled the function 'testFunction'."

显然我的方法比较复杂,参数很多,这里只是为了演示问题。

我已经编辑了它以试图澄清你的问题,但我不确定你所说的 "call a function inside action handlebars from setupController" 是什么意思。

在您包含的示例代码中,要使 testFunction 响应,它需要位于调用它的路由树的更高层。行动上去了。如果您试图在控制器上设置变量,那么在 setupController 挂钩中进行操作是正确的,但您不需要执行操作;只需在动作块外定义 testFunction ,然后像在您的路线上使用普通方法一样调用它:

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

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

我不知道 Handlebars 与这个问题有什么关系,所以我想知道我是否正确理解了您要尝试做的事情?

这应该适合你。

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

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