5

使用最新的 ember 和 ember-data。

我有一个带有项目列表的单页应用程序,并且能够在选项卡中打开项目。我可以在打开的选项卡中编辑项目,并且不提交脏记录,返回列表。

如果我刷新列表,我会收到错误消息:

Error: Attempted to handle event loadedData on <> while in state rootState.loaded.updated.uncommitted

这当然是因为我App.TestObject.find()在列表中做了一个,并且仍然有脏的未提交记录(在选项卡中打开和编辑的记录)。

我的目标是显示更新记录的列表,但对未提交的记录不做任何事情。我不想对未提交的记录进行回滚。对此有最佳做法吗?

这是一个类似的问题,但我不希望记录恢复到原始状态。 这是一个与 fiddle 类似的情况,但这里回滚是正确的解决方案。

如果我想在返回列表时忽略未提交的记录,我该如何解决?

4

2 回答 2

5

我只有通过 monkey-patching 解决这个问题DS.Model

DS.Model.reopen({
  loadedData: function() {
    if (this.get('isDirty') === false) {
      this._super.apply(this, arguments);
    }
  }
});

导致模型在处于脏状态时不更新自身,无论新 JSON 中关于此记录的内容是什么。其他记录会更新自己就好了。

于 2013-04-09T12:23:34.740 回答
4

如果你不想猴子补丁DS.Model.loadedData,这里有另一种解决方案:

App.Contact.reopenClass({
    // Results of our find() call.
    cache: null,

    // Either find our data for the first time, or refresh what we have.
    findAllWithRefresh: function () {
        if (this.cache === null) {
            this.cache = this.find();
        } else {
            this.cache.forEach(function (c) {
                // This appears to be a correct list of conditions for calling
                // refresh(), but you may need to tweak it.
                if (c.get("isLoaded") && !c.get("isSaving") &&  !c.get("isError") && !c.get("isDeleted") && !c.get("isDirty") && !c.get("isReloading")) {
                    console.log("Refreshing", c);
                    c.reload();
                } else {
                    console.log("Can't refresh", c);
                }
            });        
        }
        return this.cache;
    }
});

App.ContactsRoute = Ember.Route.extend({
    model: function (params) {
        // Note that we won't see any new records using this approach.
        return App.Contact.findAllWithRefresh();  
    }
});

这是一个有效的 jsFiddle

App.Contact.find()根本问题是当有未提交更改的记录时,您无法安全地调用。这似乎是 Ember Data 中的一个设计问题。

于 2013-04-09T12:35:47.967 回答