什么都没有处理动作' deleteComment'

时间:2017-05-21 16:41:22

标签: javascript ember.js ember-data

我有两个动作处理程序,updateComment和deleteComment,两者都以完全相同的方式爬上路径,但是updateComment工作得很好,而deleteComment没有,给出错误'没有处理动作&#39 ; deleteComment'

路线: Review.js

updateComment(comment, params) {
            Object.keys(params).forEach(function(key) {
                if(params[key]!== undefined) {
                    comment.set(key,params[key]);
                }
            });
            comment.save();
        }
    },
    deleteComment(comment) {
                comment.destroyRecord();
        }

Review.hbs

{{comment-list
    loginId=model.loginId
    article=model.article
    user=model.user
    session=model.session
    updateComment="updateComment"
    deleteComment="deleteComment"
}}

组件:comment-list.js

import Ember from 'ember';

export default Ember.Component.extend({
    actions: {

        updateComment(comment) {
            let params = {
                title: this.get('title')
            };
            this.sendAction('updateComment', comment, params);
        },
        deleteComment(comment) {
            this.sendAction('deleteComment', comment);
        }
    }
});

评论-list.hbs

    {{#each article.comments as |comment|}}
    {{comment-tile
            comment=comment
            user=user
            loginId=loginId
            updateComment="updateComment"
            deleteComment="deleteComment"

    }}
{{/each}}

comment-tile.js与comment-list.js完全相同。

评论-tile.hbs

            {{#if isAllowed}}
            {{comment-update
                    user=user
                    comment=comment
                    updateComment="updateComment"
            }}

            {{comment-delete
                    user=user
                    comment=comment
                    deleteComment="deleteComment"
            }}
        {{/if}}

comment-update.js + category-update.hbs

import Ember from 'ember';

export default Ember.Component.extend({
    updateCommentForm: false,
    actions: {
        updateCommentForm() {
            this.set('updateCommentForm', true);
        },
        updateComment(comment) {
            let params = {
                title: this.get('title'),
                body: this.get('body'),
                score: this.get('score')
            };
            this.set('updateCommentForm', false);
            this.sendAction('updateComment', comment, params);
        }
    }

});

{{#if updateCommentForm}}
    <div>
        <form>
            <div class="form-group">
                <label for="titleupdate">Titel</label>
                {{input value=comment.title id="titleupdate"}}
            </div>
            <div class="form-group">
                <label for="bodyupdate">Inhoud</label>
                {{input value=comment.body id="bodyupdate"}}
            </div>
            <div class="form-group">
                <label for="scoreupdate">Score</label>
                {{input value=comment.score id="scoreupdate"}}
            </div>

            <button class="btn-default" {{action 'updateComment' comment}}>Opslaan</button>
        </form>
    </div>
{{else}}
    <button class="btn-default btn-info" {{action 'updateCommentForm'}}>Aanpassen</button>
{{/if}}

comment-delete.js + comment-delete.hbs

import Ember from 'ember';

export default Ember.Component.extend({
    actions: {
        deleteComment(comment) {
            this.sendAction('deleteComment', comment);
        }
    }
});

<button class="btn-default btn-danger" {{action 'deleteComment' comment}}>Verwijderen</button>

它是完全相同的代码,最后只分成2个组件。对于我的生活,我无法弄清楚为什么删除操作不起作用。当我将名称从destroyComment更改为deleteComment时,我从路径到最深的组件向下工作,当我重命名最深的组件时错误发生了变化,所以我猜测它在最深层次出错了,但是我再一次,我真的不明白为什么。

编辑1:按照建议将我的代码更改为关闭操作,但同样的问题仍然存在。

编辑2:在review.js中,比我发布的代码更高级别缺少大括号。 actions:{}在updateComment之后和deleteComment之前关闭,不包括后者以供使用,但Webstorm没有抛出错误。这是包含错误的完整代码:

    actions: {
    saveComment(params) {
        let newComment = this.store.createRecord('comment', params);

        let article = params.article;
        let user = params.user;
        article.get('comments').addObject(newComment);
        user.get('comments').addObject(newComment);
        newComment.save().then(function () {
            return article.save().then(function () {
                return user.save();
            });
        });
    },
    updateComment(comment, params) {
        Object.keys(params).forEach(function (key) {
            if (params[key] !== undefined) {
                comment.set(key, params[key]);
            }
        });
        comment.save();
    },
    deleteComment(comment) {
        comment.destroyRecord();
    }
//Here should have been another curly brace.
});

1 个答案:

答案 0 :(得分:0)

在review.js中,比我发布的代码更高级别缺少大括号。 actions:{}在updateComment之后和deleteComment之前关闭,不包括后者以供使用,但Webstorm没有抛出错误。

请参阅编辑2中的代码,注意我的愚蠢。