如何撤消对未同步集合的更改?
How do I undo changed to an un-synced collection?
如何撤消对数据库的未同步更改?
一个用例场景
我想让用户在执行一个数据库操作(即删除)后至少几秒钟内撤消该操作。
一种可能性是等待从数据库中删除直到撤消它的时间过去,但是我认为在代码中反映我将在 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 集合的更改?
因此,您应该能够找回记录并将其 _status
设置为以前的旧版本,就像您正在做的那样。
问题在于 get
方法采用 includeDeleted
选项,它允许您检索已删除的记录,但是 the update
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";
不要犹豫,提交包含这些更改的拉取请求,我相信这应该可以解决您的问题!
我不确定将 'unsynced' 与 'user-can-undo' 相结合是否是一个好的设计原则。如果你确定你只想撤消删除,那么以这种方式在同步延迟上搭载你的撤消功能是可行的,但如果将来你想支持撤消更新怎么办?旧值已经丢失。
我认为您应该在您的应用程序中添加一个名为 'undo-history' 的 collection,您可以在其中存储 objects 以及撤消用户操作所需的所有数据。如果您同步此 collection,则甚至可以删除 phone 上的某些内容,然后从您的笔记本电脑上撤消该操作! :)
如何撤消对数据库的未同步更改?
一个用例场景
我想让用户在执行一个数据库操作(即删除)后至少几秒钟内撤消该操作。
一种可能性是等待从数据库中删除直到撤消它的时间过去,但是我认为在代码中反映我将在 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 集合的更改?
因此,您应该能够找回记录并将其 _status
设置为以前的旧版本,就像您正在做的那样。
问题在于 get
方法采用 includeDeleted
选项,它允许您检索已删除的记录,但是 the update
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";
不要犹豫,提交包含这些更改的拉取请求,我相信这应该可以解决您的问题!
我不确定将 'unsynced' 与 'user-can-undo' 相结合是否是一个好的设计原则。如果你确定你只想撤消删除,那么以这种方式在同步延迟上搭载你的撤消功能是可行的,但如果将来你想支持撤消更新怎么办?旧值已经丢失。
我认为您应该在您的应用程序中添加一个名为 'undo-history' 的 collection,您可以在其中存储 objects 以及撤消用户操作所需的所有数据。如果您同步此 collection,则甚至可以删除 phone 上的某些内容,然后从您的笔记本电脑上撤消该操作! :)