-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inheriting Remote Hooks #702
Comments
I have the same question like u.But it's beforeCreate.Child will inherit Parent hook. |
I am afraid this is a limitation of the current implementation of remoting hooks:
A workaround is to configure the hooks inside the /common/models/parent.js module.exports = function(Parent) {
Parent.setup = function() {
Parent.base.setup.apply(this, arguments);
this.afterRemote('**', function(ctx, result, next) {
console.log('after remote ', ctx.methodString);
next();
});
};
Parent.setup();
}; |
Seems same with a @bajtos Some questions about example: Why need to call
|
ad 1) Good catch, I have updated my comment above. It was a typo on my side. ad 2) IIRC, loopback calls only one setup method. Consider the following inheritance hierarchy: Model -> PersistedModel -> YourBase -> YourModel. When |
Loopback calls Child.setup, which is probably missing, therefore Parent.setup is invoked by searching prototype chain, as we have defined it with But why there is a call of |
AFAIK, LoopBack does not call setup function when a new model is defined. In other words, if your
No worries, I am happy to explain. Is there anything else to explain in order to make this matter more clear? |
OK. We provide setup function for Parent. It is called by Loopback when the Parent is extended. But why need to call it explicitly in a moment of definition? |
Because if you do not call it explicitly, then it won't be called for your leaf (non-base) model. You can verify this yourself by adding the following // common/models/car.json
{
"name": "Car"
}
// common/models/car.js
module.exports = function(Car) {
Car.setup = function() {
console.log('setup');
};
};
// server/model-config.json
{
"Car": {
"dataSource": "db"
}
} |
/cc @davidcheung you were investigating model inheritance recently. I think the conclusion was that we should prefer users to put stuff-to-be-inherited in the |
@bajtos yeah that was the conclusion (more info) |
LoopBack version 3 is in LTS, we won't be making breaking changes like this one. |
does setup() inheritence also works with afterRemoteError() |
I have two models:
/common/models/parent.json
/common/models/parent.js
/common/models/child.json
I would expect that the Parent's
afterRemote
hook is invoked when any of the Child's remote methods are called -- i.e. that hooks are inherited. Should I not expect this? Is there an alternate way for models to share hooks?The text was updated successfully, but these errors were encountered: