首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS-Data v3.0在生命周期钩子中停止执行

JS-Data v3.0在生命周期钩子中停止执行
EN

Stack Overflow用户
提问于 2018-10-04 21:12:33
回答 1查看 42关注 0票数 1

我使用的是js-data3.0版本,如果在保存时记录发生了变化,我会尝试阻止从update上的API接收到的记录的存储注入。

在js-datav2.9中,可以通过调用带有错误作为参数(docs)的回调来中止生命周期。

现在在3.0版本中,我使用了mapper#afterUpdate()生命周期钩子(docs),但是我不知道如何中止生命周期。

EN

回答 1

Stack Overflow用户

发布于 2018-10-04 22:47:14

显然,返回null可以防止存储注入。

用于防止update回调覆盖在save()期间对记录所做更改的完整代码

代码语言:javascript
复制
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,
  },
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52647639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档