简体   繁体   English

灰烬:将子组件的动作发送到父组件

[英]Ember: Send child component's action to a parent component

I am trying to call/trigger a child component's action from a parent. 我正在尝试从父级调用/触发子级组件的动作。 I've tried this a couple ways but I'm not getting anywhere. 我已经尝试了几种方法,但是我什么都没得到。 This is basically what I have so far. 到目前为止,这基本上就是我所拥有的。
I am attempting to send the action up to the parent by passing it as a parameter in the action that was passed down... 我试图通过将动作作为参数传递给向下传递的动作来将动作发送给父项...
This seems a bit like a spaghetti situation but the way my project is organized, this is my result. 这似乎有点像意大利面条的情况,但是我的项目组织方式是我的结果。

If anyone could, please tell me how to successfully pass an action up to the parent as a parameter. 如果有人可以,请告诉我如何成功地将操作作为参数传递给父级。 (I'm curious to know how, if possible) (我很好奇,如果可能的话)
If anyone also has a better suggestion on how to call a child's action, please share. 如果有人对如何调用孩子的动作还有更好的建议,请分享。 Thanks in advance! 提前致谢!

parent-component.hbs 家长component.hbs

{{child-component anAction=(action "anAction")}}
<div onclick={{action actionPassedFromChild}}></div>

parent-component.js 家长component.js

actionPassedFromChild: null,
....
actions: {
    parentAction(childAction){
       this.set('actionPassedFromChild', childAction);
    }
}


child-component.hbs 儿童component.hbs

<div onclick={{action "anAction"}}

child-component.js 儿童component.js

....
actions: {
    anAction(){
       this.parentAction(childAction);
    }
    childAction(){
       //Some operation
    }
}

In this example, if I stop the code inside of 'anAction', I do have 'childAction'. 在此示例中,如果我停止在“ anAction”内部的代码,则确实具有“ childAction”。 But by the time that it gets passed into 'parentAction', it is undefined. 但是,当它传递给“ parentAction”时,它是不确定的。 Could anyone explain why? 谁能解释为什么?

It seems like you have some typos. 看来您有错别字。 For example, parentAction is not passed to child component. 例如,parentAction不传递给子组件。 But if I understood what do you want to achieve right - it is doable, however I can't even imagine why you might need this. 但是,如果我了解您想要实现的目标是正确的-这是可行的,但是我什至无法想象您为什么需要这样做。

You can play with my example here . 您可以在此处使用我的示例。 Select is in child component and button is in parent component. 选择位于子组件中,而按钮位于父组件中。 When you choose something in select - child component sends one of two functions to parent component. 当您在select-中选择某些内容时,子组件会将两个函数之一发送给父组件。 And when you click a button - parent component calls that function. 当您单击按钮时,父组件将调用该函数。

Code: 码:

//child-component.js
import Ember from 'ember';


export default Ember.Component.extend({
  line1: "Say Hi!",
  line2: "Say Yeah!",

  changeAction(newAction) {
    switch (newAction) {
      case "1":
        this.onActionChange(this.action1);
        break;

      case "2":
        this.onActionChange(this.action2);
        break;

      default:
        this.onActionChange(undefined);
        break;
    }
  },

  action1: Ember.computed(function(){
    const that = this;
    return function() {
      alert(that.line1);
    }
  }),

  action2: Ember.computed(function(){
    const that = this;
    return function() {
      alert(that.line2);
    }
  })
});

//child-component.hbs
<select  onchange={{action changeAction value="target.value"}}>
        <option>Choose something</option>
        <option value="1">Action 1</option>
    <option value="2">Action 2</option>
</select>

//parent-component.js
import Ember from 'ember';

export default Ember.Component.extend({
  childishAction() {
    if (typeof this.childAction === 'function') {
      this.childAction();
    }
  }
});

//parent-component.hbs
{{child-component onActionChange=(action (mut childAction))}}
<div><button disabled={{unless childAction true false}} onclick={{action childishAction}}>Let's do it!</button></div>

What happens here - you can't pass to action helper something that is not defined when ember renders template. 这里发生的事情-您无法将在ember渲染模板时未定义的内容传递给action助手。 So you need to store action that child-component sends into some variable and call it using some intermediate action of parent component. 因此,您需要将子组件发送的操作存储到某个变量中,并使用父组件的某些中间操作对其进行调用。

In my example, function returned from child component is stored in childAction property of parent component and childishAction of parent component calls it. 在我的示例中,从子组件返回的函数存储在父组件的childAction属性中,父组件的childishAction进行调用。

Hope this helps. 希望这可以帮助。 But you probably trying to solve some problem in not a right way. 但是您可能试图以不正确的方式解决一些问题。

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

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