我使用的是js-data3.0版本,如果在保存时记录发生了变化,我会尝试阻止从update上的API接收到的记录的存储注入。
在js-datav2.9中,可以通过调用带有错误作为参数(docs)的回调来中止生命周期。
现在在3.0版本中,我使用了mapper#afterUpdate()生命周期钩子(docs),但是我不知道如何中止生命周期。
发布于 2018-10-04 22:47:14
显然,返回null可以防止存储注入。
用于防止update回调覆盖在save()期间对记录所做更改的完整代码
function beforeUpdate(id, props, opts) {
const currentStoreItem = this.datastore.get(opts.name, id)
opts.tlChangesBeforeUpdate = JSON.stringify(currentStoreItem.changes())
return this.constructor.prototype.beforeUpdate.call(this, id, props, opts)
}
function afterUpdate(id, props, opts, result) {
const currentStoreItem = this.datastore.get(opts.name, id)
const currentChanges = JSON.stringify(currentStoreItem && currentStoreItem.changes())
if (currentChanges != opts.tlChangesBeforeUpdate) return null // This prevents store injecton
return this.constructor.prototype.afterUpdate.call(this, id, props, opts, result)
}
const ds = new DataStore({
mapperDefaults: {
beforeUpdate: beforeUpdate,
afterUpdate: afterUpdate,
},
})https://stackoverflow.com/questions/52647639
复制相似问题