我如何能够撤销对数据库的非同步更改?
用例场景
我想让用户在执行一个数据库操作后至少几秒钟内撤销数据库操作(即删除)。
一种可能是保留数据库中的删除,直到撤销它的时间过去,但是我认为它会更精简,以反映我将在UI中看到的内容,只为了保持1:1。
因此,我尝试在删除之前存储对象,然后更新它(这样它的_status就不会再被删除了):
this.lastDeletedDoc = this.docs[this.lastDeletedDocIndex];
// remove from the db
this.documents.delete(docId)
.then(console.log.bind(console))
.catch(console.error.bind(console));
// ...
// user taps "UNDO"
this.documents.update(this.lastDeletedDoc)
.then(console.log.bind(console))
.catch(console.error.bind(console));但是我得到了错误Error: Record with id=65660f62-3eb1-47b7-8746-5d0b2ef44eeb not found。
我还尝试使用以下方法再次创建该对象:
// user taps "UNDO"
this.documents.create(this.lastDeletedDoc, { useRecordId: true })
.then(console.log.bind(console))
.catch(console.error.bind(console));但是我得到了一个Id already present错误。
我还快速浏览了源代码,但找不到任何undo函数。
一般情况下,我如何撤销对未同步的kinto集合的更改?
发布于 2016-03-28 20:23:08
因此,您应该能够找到该记录,并将其_status设置为以前的版本,就像您正在做的那样。
问题在于get方法采用了一个includeDeleted选项,允许您检索已删除的记录,但是可以检索 method doesn't pass it this option。
解决这一问题的最佳方法可能是在Kinto.js存储库上打开一个拉请求,使update方法接受一个includeDeleted选项,该选项将传递给get方法。
由于连接有限,我现在无法推动更改,但它基本上是这样的(+一个测试,它演示了这一点是否正常工作):
diff --git a/src/collection.js b/src/collection.js
index c0cce02..a0bf0e4 100644
--- a/src/collection.js
+++ b/src/collection.js
@@ -469,7 +469,7 @@ export default class Collection {
* @param {Object} options
* @return {Promise}
*/
- update(record, options={synced: false, patch: false}) {
+ update(record, options={synced: false, patch: false, includeDeleted:false}) {
if (typeof(record) !== "object") {
return Promise.reject(new Error("Record is not an object."));
}
@@ -479,7 +479,7 @@ export default class Collection {
if (!this.idSchema.validate(record.id)) {
return Promise.reject(new Error(`Invalid Id: ${record.id}`));
}
- return this.get(record.id)
+ return this.get(record.id, {includeDeleted: options.includeDeleted})
.then((res) => {
const existing = res.data;
const newStatus = options.synced ? "synced" : "updated";不要犹豫,提交一个拉请求与这些变化,我相信,应该解决您的问题!
发布于 2016-03-29 03:14:43
我不确定合并'unsynced‘和’用户可以撤销‘是否是一个好的设计原则。如果您确信您只想撤消删除,那么它就可以以这种方式在同步延迟上恢复您的撤消功能,但是如果将来您想要支持撤消更新呢?旧的价值已经失去了。
我认为您应该在应用程序中添加一个名为“撤销历史记录”的集合,在这个集合中,您可以使用撤消用户操作所需的所有数据来存储对象。如果您同步这个集合,那么您甚至可以删除手机上的某些内容,然后从您的膝上型计算机中撤消它!)
https://stackoverflow.com/questions/36259550
复制相似问题