如何跟踪未同步到服务器的模型更改?
我有一个表单,它通过执行许多 set
方法的更改事件来更新我的模型。
model.set({Name: newname });
使用 Derick Bailey 出色的模型绑定插件也可以自动实现同样的效果。
不管怎样,模型在改变形式的同时也在不断更新。但是,除非我单击实际执行 model.save();
调用的保存按钮,否则这些更改都不会进入数据库。
因此,如果我现在通过以下方式导航离开表单:
- 浏览器中的后退按钮
- 导航中的其他按钮 打开另一个视图
就会立即清楚,模型已使用新名称保存,但从未保存到数据库中(使用以下命令刷新) F5 显示)
我正在寻找解决此问题的有用解决方案; 到目前为止,我一直在考虑:
- 在表单元素的任何模糊事件上添加
model.save()
(仍然会导致仅针对 1 个模型*对服务器进行多次保存调用理想 - 尝试在表单退出时进行保存*不理想
- 尝试将模型恢复到其原始状态(模型重置或类似)
*不理想 :用户确实离开了表单,表明他不希望保存它的数据,在这里我试图破解保存,无论如何
我将如何实现将模型重置为更改表单之前的状态?
I have a form, that updates my model with change events executing a lot of set
methods.
model.set({Name: newname });
The same could be achieved automatically with Derick Bailey's wonderful modelbinding plugin.
Anyway, the model is updated constantly when changing the form. BUT, unless I click the save button which actually executes a model.save();
call, none of these changes will ever make it into the database.
Thus, if I now navigate away from the form via:
- back button from the browser
- other buttons in navigation opening another view
it is immediately clear, the model was saved with it's new name, yet it was never saved to the database, (refreshing with F5 shows that)
I'm looking for a useful solution to this problem;
so far I've been thinking of:
- adding a
model.save()
on any blur event of a form element (would still cause many save calls to the server for just 1 model *not ideal - trying to hack in a save on the exit of the form *not ideal
- trying to restore the model to it's original state (model reset or similar)
*not ideal : user does navigate away from the form, indicating his wish NOT to save it's data, and here i am trying to hack in a save anyway.
How would I achieve this resetting the model to it's state it was in before changing the form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果需要,您可以保存模型的初始状态。但您正在寻找的真正解决方案是调用
set
< /a> 而不是保存
。它将执行您的验证,而无需同步到持久层(即数据库)。它还会触发change
事件,以便您的视图可以做出相应的反应。然后,当您准备好保存时,就可以进行保存。If you want, you could save the model's initial state. But the real solution that you're looking for is to call
set
instead ofsave
. It will perform your validation without syncing to your persistence layer (i.e., the database). It will also trigger thechange
event so your view can react accordingly. Then, when you're ready to save, you can.