在Bookshelf.js模型中的属性更新时,我该如何收听?

时间:2018-09-19 19:17:19

标签: node.js orm bookshelf.js

我正在使用Bookshelf.js。我想听听我的一个模型中的特定属性何时更新。如果是这样,我想运行一个函数。如何收听模型的更新?

1 个答案:

答案 0 :(得分:1)

例如:

const User = bookshelf.Model.extend({
  tableName: 'users',

  initialize() {
    this.on('updated', (model) => {
      // This is fired after a model is updated
    })
  }
})

请注意,这不会在您每次使用.set()更改模型上的属性时触发,而是仅在使用model.save()将模型保存到数据库时才会触发。