2013-04-04 85 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