【问题标题】:Sending an action from a sub component to parent component in Ember 2.2在 Ember 2.2 中将动作从子组件发送到父组件
【发布时间】:2016-02-07 16:15:01
【问题描述】:

您好,我正在尝试将子组件中的操作发送回父组件,以便它可以访问 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>

在 app/components/item-listing.js 中,我必须定义一个操作,否则我会收到一个操作未定义错误。从这里 this.store 是未定义的,所以我试图将动作冒泡。

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

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

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

但它似乎永远无法通过 sendAction 调用。我做错了什么?

【问题讨论】:

    标签: javascript ember.js ember-components


    【解决方案1】:

    你必须:

    1. 在您的控制器 (ItemIndexController) 中定义该操作 (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}}
    

    【讨论】:

    • 谢谢!不得不使用第二种方式。
    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2020-09-28
    • 2017-07-06
    • 2021-08-24
    • 2019-09-07
    • 2018-07-07
    相关资源
    最近更新 更多