销毁Ember数据记录时出错

时间:2017-02-07 01:34:25

标签: javascript ember.js ember-data

我在删除Ember数据记录时遇到错误,我无法找出原因,

Attempted to handle event `pushedData` on <card:32> while in state root.deleted.inFlight. 
Error

以下代码是触发错误的地方,

    deleteBtnClicked() {
        const controller = this.controllerFor('browse/index');
        let selectedCards = controller.getSelectedCard();
        selectedCards = selectedCards.map((card) => {
            return parseInt(Ember.get(card, 'id'));
        });

        selectedCards.forEach((id) => {
            this.store.find('card', id)
                .then((card) => {
                    card.destroyRecord();
                });
        });
    }

原来记录已被成功删除,但错误仍然存​​在。

1 个答案:

答案 0 :(得分:1)

find is private method所以请不要使用它。相反,您可以使用findAllfindRecord

deleteBtnClicked() {
    const controller = this.controllerFor('browse/index');
    let selectedCards = controller.getSelectedCard();
    //if selectedCards itself is RecordArray you can easily iterate using forEach and destroyRecord. no need of findRecord.
    selectedCards = selectedCards.map((card) => {
        return parseInt(Ember.get(card, 'id'));
    });
    //I added reload: true this will ensure promise will be resolved only after adapters returns the data.
    selectedCards.forEach((id) => {
        this.store.findRecord('card', id, { reload: true })
            .then((card) => {
                card.destroyRecord();
            });
    });
}