尝试处理事件“loadedData"时出错:deleteRecord 后对象未更新 [英] Error Attempted to handle event `loadedData` : Object not updated after deleteRecord

查看:22
本文介绍了尝试处理事件“loadedData"时出错:deleteRecord 后对象未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最新版本的 ember-js 和 ember-data,删除记录时出现问题.

Using the last version of ember-js and ember-data, I got an issue when deleting a record.

我的路线:

App.ListContactsRoute = Em.Route.extend({
    model: function() {
        App.Contact.find();
    },
    setupController: function(controller, model) {
        controller.set('contacts', model);
    }
});

App.EditContactRoute = Em.Route.extend({
    setupController: function(controller, model) {
        this.transaction = controller.get('store').transaction();
        this.transaction.add(model);
        controller.set('content', model);
        controller.set('organizations', App.Organization.find());
    },
    events: {
        delete: function(contact) {
            contact.deleteRecord();
            this.transaction.commit();
            this.transaction = null;
            this.transitionTo("listContacts");
        },
        save: function(contact) {
            this.transaction.commit();
            this.transaction = null;
            this.transitionTo("editContact", contact);
        }
    }
});

删除联系人时,我将返回到 ListContactsRou​​te,因此调用 API 返回联系人列表.此时,删除的联系人在服务器上还没有被删除.

When deleting a contact, I'm going back to the ListContactsRoute, so a call is made to the API wich returns me a list of contacts. At this point, the deleted contact hasn't been deleted on the server yet.

结果,已删除的联系人仍然存在于我的联系人列表模板中.这是错误:

In result, the deleted contact is still present on my contacts list template. Here's the error :

"Uncaught Error: Attempted to handle event `loadedData` on <App.Contact:ember469:null> while in state rootState.deleted.inFlight. Called with undefined"

我做错了什么还是有办法解决这个问题?

Am I doing something wrong or is there a way to fix this ?

推荐答案

该记录不再是 this.transaction 的一部分,一旦你提交一个事务,一条记录就会移动到商店默认的事务中.要反映您的删除操作,您需要提交存储.

The record is no longer part of the this.transaction, once you commit a transaction a record is moved to the store default transaction. To reflect your deletion action, you need to commit the store.

contact.deleteRecord()
App.store.commit();

这篇关于尝试处理事件“loadedData"时出错:deleteRecord 后对象未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆