0

我觉得这是一个非常愚蠢的问题,但是尽管我尝试了很多,但我并没有更接近于自己解决它;

所以,我们开始:

我安装和设置虚拟插件:

bookshelf.plugin('bookshelf-virtuals-plugin');

然后定义getter:

...bookshelf.model(
  'MasterModel',
  {
    virtuals: {
      type() { return this.constructor.name; }
    }
  ...
  });
...

稍后,我扩展了这个主模型并定义了其他模型。希望能够通过以下方式检索当前变量是其实例的链中最后一个模型的名称:instance.get('type').

我认为书架的对象上必须有一个属性/方法Model来访问在定义期间定义的那个,但是在 api 文档中找不到一个!

PS。我使用虚拟是因为我希望跳过toJSON().

4

1 回答 1

0

如果你只想确定一个模型是否属于某种类型,你只需要这个:

const MyModel = bookshelf.model('MyModel')

// next some place where you need to check the model type
if (anotherModel instanceof MyModel) {
  // do stuff
}
于 2020-05-06T12:02:19.497 回答