我的模板中有一个保存按钮,该按钮在模型具有 DirtyAttributes 时激活
当对相关模型的引用更改时,似乎没有设置 hasDirtyAttributes 标志。
例子
我有一个下拉菜单,允许选择名为contact
的相关模型
如果我更改任何直接属性(例如名称),一切都会按预期工作,并且保存按钮会激活。
当我更改联系人时,它没有,我认为这是设计使然,所以我在触发更改操作时设置了标志。
我在我的路线 Action 中这样设置:
actions:{
updateProductionContact: function(contact){
this.set('currentModel.customer.hasDirtyAttributes',true);
this.set('currentModel.customer.production_contact',contact);
},
}
现在又可以用了。当我更改联系人时,保存按钮会亮起。
但是,当我现在单击保存时,hasDirtyAttributes 标志保持为真(按钮保持事件状态),而之前它被清除,直到进行另一次更改。
我希望框架在成功保存后自动重新设置标志,就像以前一样。我当然可以在按钮的保存操作上重新设置标志。
感觉就像我在绕过一个问题,也许 hasDirtyAttributes 不应该手动设置,或者我应该使用不同的脏指标。
我的问题:如何正确处理?
最佳答案
hasDirtyAttributes
是DS.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
关于ember.js 手动设置 hasDirtyAttributes 保存时未清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572718/