如果在对话框中使用数据绑定,当用户取消编辑时如何恢复数据? [英] If use databinding in a dialog, how to restore data when user cancelled the editing?

查看:29
本文介绍了如果在对话框中使用数据绑定,当用户取消编辑时如何恢复数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我得到一个 bean 和一个对话框,并且它们与双向数据绑定相结合,那么当用户取消编辑时回滚到原始 bean 的最佳方法是什么.

If I get a bean and a dialog, and they are coupled with bidirectional data-binding, what is the best way to roll back to the original bean when user canceled the editing.

编辑 1

如果用户在编辑模式下打开对话框,然后他做了一些修改并按下确定",然后这个对话框关闭并且底层 bean 得到更新.当我说取消编辑"时,我的意思是用户打开对话框并进行了一些修改,但按下了取消"按钮.在这种情况下,底层 bean 应该保持不变,但是由于数据绑定,它变脏了,我想要原始 bean.

If user opened up the dialog in edit mode, he then did some modification and pressed "OK", then this dialog closed and the underlying bean got updated. When I said "canceled the editing", I mean the user opened up the dialog and did some modification but pressed "cancel" button. In this case, the underlying bean should keep untouched, but due to data-binding, it become dirty, I want the original bean back.

我可以在对话框打开时克隆一个 bean,如果用户按确定",克隆的 bean 将被复制回原始 bean;如果用户按取消",克隆的 bean 将被放弃.我不知道这是不是一个好方法.

I can just clone a bean when the dialog opens, if user presses "OK" the cloned bean will be copied back to original bean; if user presses "cancel" the cloned bean will be abandoned. I don't know if this is a good approach.

推荐答案

我一直非常成功地使用克隆方法.克隆方法有两种:绑定​​到克隆和绑定到原始.

I have always used the clone approach quite successfully. The clone approach comes in two varieties: bind to clone and bind to original.

绑定到克隆将使它的任何其他绑定到屏幕上的同一字段在您的对话框打开时不会更新.当按下 OK 时,您将克隆复制到原始对象,取消时您只需将克隆扔掉.

Bind to clone will make it so any other binding to the same field on the screen will not update while your dialog box is up. When OK is pressed you copy the clone to the original object, on cancel you simply throw the clone away.

绑定到原始允许屏幕更新绑定到同一字段的其他组件.当按下 OK 时,您将克隆扔掉.当按下取消时,您将克隆复制到原始.

Bind to original allows screen updates to others components bound to the same field. When OK is pressed you throw the clone away. When cancel is pressed you copy the clone to the original.

我喜欢绑定到克隆方法,因为我认为在对话框出现时看到其他屏幕组件更新会令人困惑.我认为取消是否会回滚出现在对话框外的更改会造成混淆.

I favor the bind to clone approach since I think it is confusing to see other on screen components updating while a dialog box is up. I think it creates confusion as to whether cancel will rollback changes that are appearing outside the dialog box.

另一种方法是使用可刷新绑定策略,在这种策略中,可以以这样一种方式设置绑定,即在调用某种flush() 方法之前它不会更新对象.如果您的数据绑定框架不支持这一点,那么以后要解决这个问题可能需要做很多工作.

The alternative is to use a flushable binding strategy where a binding can be set up in such a way that it will not update the object until some kind of flush() method has been called. If your databinding framework does not support this then it can be a lot of work to tack this on later.

这篇关于如果在对话框中使用数据绑定,当用户取消编辑时如何恢复数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆