2017-06-15 74 views
1

自動清零我有我的模板保存按鈕激活時模型hasDirtyAttributesember.js手動設置hasDirtyAttributes不保存

的hasDirtyAttributes標誌似乎並沒有被設置時,相關模型的引用變化。


我有一個下拉菜單,允許
如果我改變任何直接的屬性(如名稱),一切正常採摘稱爲接觸的相關模型,並保存按鈕激活。
當我更改聯繫人時,它不會,我假設這是設計,所以我在更改操作被觸發時設置了標誌。

我在我的路線行動,像這樣設置的:

actions:{ 

    updateProductionContact: function(contact){ 
     this.set('currentModel.customer.hasDirtyAttributes',true);    
     this.set('currentModel.customer.production_contact',contact); 
    }, 
} 

現在它再次工作。當我更換聯繫人時,保存按鈕亮起。
但是,當我現在單擊保存時,hasDirtyAttributes標誌保持爲真(按鈕保持活動狀態),而之前它被清除,直到做出其他更改。

我希望框架在成功保存後自動重新設置標誌,就像以前一樣。我當然可以在按鈕的保存動作上重新設置標誌。

感覺就像我在解決問題的方法一樣,也許有不應該手動設置的DirtyAttributes,或者我應該使用不同的髒度指標。

我的問題:如何正確處理這個問題?

+0

是的,你應該使用'hasDirtyAttributes'根據持久化記錄指南檢查一個值是否髒(https://guides.emberjs.com/v2.13.0/models/creating-updating-and-deleting-records /#toc_persisting-記錄)。但是,我不確定爲什麼在保存後髒仍然存在。您可能需要在餘燼鬆弛 一般頻道中尋求更多幫助。 – AlexMA

回答

1

hasDirtyAttributesDS.Model的計算屬性,所以如果你設置它,你不應該手動設置它,那麼下次它不會被重新計算。如果屬性有任何變化,它將被更新。

像Alexma在評論中建議的那樣,您可以使用dirtyAttributes。請參閱https://guides.emberjs.com/v2.13.0/models/creating-updating-and-deleting-records/#toc_persisting-records 但不要自己設置。

參見: https://github.com/emberjs/data/blob/v2.13.0/addon/-private/system/model/model.js#L162

+0

我已經閱讀過該頁面。您的源代碼鏈接清楚地表明hasDirtyAttributes的確是一個計算屬性。在餘燼檢查中,它顯示爲一面旗幟,這就是爲什麼我決定我可以設置它。 – Loopo

0

原來hasDirtyAttributes是函數/運算屬性。所以使用set(...,true)會用布爾值覆蓋函數,這不是我們想要的。

有一種方法可以讓ember中的計算屬性擁有setter和getters,但這似乎並沒有在這裏實現。

我想出了以下解決方案。

基本上這實現了相關模型的單獨的自定義標誌。

在路線的模型屬性我已經定義了一個附加屬性:

model: function(params) { 
    return Ember.RSVP.hash({ 
     customer: this.store.findRecord('customer',params.id), 
     .... 
     dirtyRelations: false 
    }); 
}, ... 

然後我手動設置此當相關模型改變

updateProductionContact: function(contact){ 
    this.set('currentModel.dirtyRelations',true);    
    ... 
}, ... 

我的保存功能,將其設置爲false 。

updateCustomer: function(){ 
    this.get('currentModel.customer').save(); 
    this.set('currentModel.dirtyRelations',false); 
} 

我對任何hasDirtyAttributes template.hbs支票或dirtyRelations

{{#if (or model.customer.hasDirtyAttributes model.dirtyRelations)}} 
    highlighted save 
{{else}} 
    plain save 
{{/if}} 

到目前爲止,這似乎運作良好。我可以利用正常屬性的自動髒跟蹤功能。