2017-06-15 26 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}} 

到目前为止,这似乎运作良好。我可以利用正常属性的自动脏跟踪功能。