简体   繁体   中英

Delegating afterRemote() to related class in Strongloop Loopback

With my Loopback project I have a model (ModelA) which references a property of type ModelB. For example (from common/models/modela.json):

  "relations": {
    "modelbs": {
      "type": "hasMany",
      "model": "ModelB",
      "foreignKey": "ownerId"
    }
  }

I would like to use ModelB.afterRemote (from modelb.js) to filter some properties in response to GET /api/modelas/1/modelbs . However, ModelB.afterRemote is never called. Instead, ModelA.afterRemote (from modela.js) is called with ModelB objects in ctx.result.

How do I implement an afterRemote for ModelBs without injecting the code into each top-level class that might happen to have relationships with ModelBs?

For reference, my call signature for afterRemote is,

   ModelB.afterRemote('**', function (ctx, unused, next) { ... }

You might be able to do the filtering using default scope for a model. It is basically a filter object that applies to all queries to the model. See more information at http://docs.strongloop.com/display/LB/Model+definition+JSON+file#ModeldefinitionJSONfile-Defaultscope .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM