0

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) { ... }
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
doublemarked
  • 1,920
  • 1
  • 13
  • 10

1 Answers1

0

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.

Raymond Feng
  • 1,516
  • 9
  • 5