2013-04-04 79 views
5

使用最新的余烬和余烬数据。从服务器刷新列表时,如何忽略脏记录?

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

如果我刷新列表,我得到的错误:

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

这是当然的,因为我已经做了列表中的一个App.TestObject.find(),而且还有脏未提交的记录(打开和编辑记录在标签中)。

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

This is a similar question,但我不希望记录恢复到原始状态。 This is a similar case with a fiddle,但这里回滚是正确的解决方案。

如何在我回到列表中时忽略未提交的记录来解决小提琴问题?

回答

5

我只有通过猴修补DS.Model解决此问题的解决方法。

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

使模型在脏状态时不更新自身,无论新记录中的JSON如何。其他记录会自动更新。

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(); 
    } 
}); 

这里有一个working jsFiddle

潜在的问题是,如果存在未提交更改的记录,则无法安全地调用App.Contact.find()。这看起来像Ember Data中的一个设计问题。

+1

似乎这种方法会在服务器上产生大量的XHR。考虑一个150个元素的列表,其中零个或只有一个记录是脏的,这将导致149或150个XHR ... – 2013-04-22 21:56:34