简体   繁体   English

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

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

Hi I'm trying to send an action from a sub component back up to the parent component so it can access this.store and perform a DB action. 嗨,我正在尝试将操作从子组件发送回父组件,以便它可以访问this.store并执行数据库操作。 The basic layout is this: 基本布局是这样的:

app/templates/item/index.hbs -> does a loop of items using component app / templates / item / index.hbs->使用组件进行项目循环

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

app/templates/components/item-listing.hbs 应用程序/模板/组件/项,listing.hbs

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

In app/components/item-listing.js I have to define an action or I get an action not defined error. 在app / components / item-listing.js中,我必须定义一个动作,否则会收到未定义的动作错误。 From here this.store is undefined, so I'm trying to bubble the action up. 从这里this.store是未定义的,所以我试图将动作冒泡。

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

From here I'm lost. 从这里我迷路了。 I've tried putting actions on all of the following: 我尝试对以下所有内容进行操作:

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

But it never seems to get past the sendAction call. 但是它似乎永远不会超越sendAction调用。 What am I doing wrong? 我究竟做错了什么?

You have to: 你必须:

  1. Define that action ( copyItem ) in your controller (ItemIndexController). 在控制器(ItemIndexController)中定义该动作( copyItem )。
  2. Pass that action in template loop: 在模板循环中传递该动作:

1st way: 第一种方式:

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

2nd way: 第二种方式:

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

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

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