2012-07-30 64 views
6

爲了讓我的應用程序的頂部有一個黃色的「保存」/「保存」指示器消息,我想要一個布爾屬性,指示是否任何燼數據記錄目前正在飛行中。燼數據:如何使一個保存/保存的Flash消息

我嘗試這樣做:

App.store = DS.Store.create 
    isSaving: (-> 
    for record in this.get('recordCache') 
     if record?.getPath('stateManager.currentState.name') == 'inFlight' 
     return true 
    return false 
).property('[email protected]') 

,但後來我發現,recordCache是無法觀測。

我不使用交易,只有App.store.commit(),所以我看着App.store.get('defaultTransaction'),但它沒有產生任何有用的東西。

我正在使用RESTAdapter,所以如果我可以將它擴展爲給我這條信息,那也可以。

+0

是defaultTransaction.buckets.inflight可觀察嗎? – 2012-07-31 16:15:04

+0

你有沒有想過什麼?我正在構建一個應用程序的解決方案,但我不喜歡它。它擴展了DS.Model類,並設置屬性didUpdate()在保存模型時觸發通知。 – 2012-09-13 22:02:00

+0

不,我沒有。你爲什麼不把解決方案作爲答案發布,即使它是一個缺口? – 2012-09-14 12:25:38

回答

2

那麼,你可以簡單地創建一個基本模型,它具有處理顯示通知消息的didUpdate屬性,然後讓每個模型擴展它。這的確是一個權宜之計,但它的工作原理暫且:

App.Model = DS.Model.extend 
    didUpdate: -> 
     # invoke your notification 
     alert("All changes saved!") 

App.User = App.Model.extend() 

但是....這將觸發該更新每個記錄。所以你可能希望創建你的通知類,以便它不會一次打開多個保存的通知......但這取決於你。

我目前正致力於從這個拉請求https://github.com/emberjs/data/pull/376實現一些基本的服務器驗證方法,我希望我會想出一個更好的方式來觀察提交的狀態(以及將如果我這樣做課程回到這裏)。

4

爲什麼不延長模型,控制器和/或視圖:

DS.Model.reopen({ 
    didCreate: function() { 
     this.set('persisted', true); 
    }, 
    didUpdate: function() { 
     this.set('persisted', true);  
    }, 

    resetPersistenceFlag: function(){ 
     this.set('persisted', false); 
    }.observes('isDirty') 

}); 

Ember.Controller.reopen({ 
    contentPersistedObserver: function() { 
     if (this.get('content.persisted') === true) { 
      //do stuff here 
     } 
    }.observes('content.persisted') 
}); 

Ember.View.reopen({ 
    controllerContentPersistedObserver: function() { 
     if (this.get('controller.content.persisted') === true) { 
      //do stuff here 
     } 
    }.observes('controller.content.persisted') 
});​ 

這樣被保存模型時,控制器/圖就知道了。它會爲創建/更新控制器啓動,但對於管理陣列的控制器則不會。

+0

我真的很喜歡這個,並且把東西帶回他們所屬的控制器。幾乎可以開始做簡單的錯誤處理了。仍然會有問題的地方控制器將觸發更新每個記錄...所以在我的情況下,如果我有10個記錄一次更新我得到10個警報窗口......但這超出了這個問題的範圍,我認爲。 – 2012-12-18 06:36:11

+0

這有點不對,因爲didCreate立即觸發,所以並不意味着持續。 didUpdate是您需要將持久保持爲true的所有設置。 – jhorman 2012-12-19 19:29:15

0

還有一個更簡單的解決了這個:

App.MyView = Ember.View.extend({ 

    init: function() { 
     this._super(); 
     var self = this; 
     this.get('controller').content.on('didUpdate', function() { 
      self.SomeCoolUpdateFunctionForMyView(self.$()); 
     }); 
    } 
}); 

自我視圖的jQuery的表示$()引用。