在 Ember 2.2 中将操作从子组件发送到父组件

Sending an action from a sub component to parent component in Ember 2.2

本文关键字:组件 Ember 操作      更新时间:2023-09-26

嗨,我正在尝试将操作从子组件发送回父组件,以便它可以访问 this.store 并执行数据库操作。基本布局如下:

app/templates/item/index.hbs -> 使用组件执行项目循环

            {{#each model as |item|}}
                {{item-listing item=item}}
            {{/each}}

app/templates/components/item-listing.hbs

<li><a {{action 'copyItem' item}}>Copy</a></li>

在应用程序/组件/项目列表中.js我必须定义一个操作,否则我会收到一个未定义操作的错误。从这里开始,this.store 是不确定的,所以我试图冒泡动作。

actions: {
    copyItem: function(item) {
        this.sendAction('copyItem', item);
    },

从这里我迷路了。我尝试对以下所有内容进行操作:

/

app/routes/item/index.js/app/routes/item.js

但它似乎从未通过发送行动调用。我做错了什么?

您必须:

    在控制器(
  1. 项目索引控制器(中定义该操作(copyItem(。
  2. 在模板循环中传递该操作:

第一种方式:

{{#each model as |item|}}
    {{item-listing item=item copyItem='copyItem'}}
{{/each}}

第二种方式:

{{#each model as |item|}}
    {{item-listing item=item copyItem=(action 'copyItem')}}
{{/each}}