【问题标题】:Calling controller function from Ember setupController inside Route从 Route 内的 Ember setupController 调用控制器函数
【发布时间】:2019-10-22 04:34:39
【问题描述】:

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

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

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

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

这会返回一个错误:“没有处理函数'testFunction'。”

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

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    这应该适合你。

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

    【讨论】:

      【解决方案2】:

      我已对此进行了编辑以试图澄清您的问题,但我不确定您所说的“从 setupController 调用操作手柄内的函数”是什么意思。

      在您包含的示例代码中,为了让testFunction 做出响应,它需要位于调用它的路由树的更高位置。行动上去了。如果您尝试在控制器上设置变量,您可以在 setupController 挂钩中执行此操作,但您不需要执行此操作;只需在操作块之外定义testFunction,并像在您的路线上使用普通方法一样调用它:

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

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

      【讨论】:

      • 我和他有同样的问题,所以你在 setupController(controller,....
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多