繁体   English   中英

如何(如果可能)在 Javascript 过程或 function 中调用 Ember 操作?

[英]How to (if possible) call an Ember Action in a Javascript procedure or function?

我的问题是在 Ember 2.1 中与 Javascript 过程在同一文件中定义的 Ember 操作。

例如,在这里,我们希望在“导出”区域之外调用 myAction 的 ember 代码:


export default Component.extend({
 
  actions: {
    myAction() {
    }
  }
});  

function test(x) {
  CALL TO myAction HERE
  return(3x);
}

在现代 ember 中,动作只是函数,所以你可以调用它们:

export default class MyComponent extends Component {
  myFunc() {
    this.myAction(); // this just works
  }

  @action
  myAction() {
  }
}

在较旧的 embers 经典课程中有send或者您可以访问该操作并.call它。

export default Component.extend({
  myFunc() {
    this.send('myAction'); // this calls the action
    this.actions.myAction.call(this); // this calls the action
  }
  actions: {
    myAction() {
    }
  }
});

暂无
暂无

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

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