You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
Seems same with a remoteMethod as well. It only works if defined inside overloaded setup method in parent model.
@bajtos Some questions about example: /common/models/parent.js
1. Parent.base.apply(this, arguments); - doesn't work Parent.base.setup.apply(this, arguments); - works
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 YourModel is created, loopback will call YourBase.setup and it's up to that method to call PersistedModel.setup.
Sorry for annoyance. It's still not clear to me.
In our example we have inheritance chain: Model -> PersistedModel -> Parent -> Child
Loopback calls Child.setup, which is probably missing, therefore Parent.setup is invoked by searching prototype chain, as we have defined it with Parent.setup = function() { ... .
In Parent.setup we call base class method with Parent.base.setup.apply(this, arguments); which is defined in PersistedModel.setup. That's fine.
But why there is a call of Parent.setup(); at the end of an example. Why to call itself immediately after definition?
But why there is a call of Parent.setup(); at the end of an example. Why to call itself immediately after definition?
AFAIK, LoopBack does not call setup function when a new model is defined. In other words, if your Child model provides a setup function, it is called only when somebody extends (inherits from) your Child, but not when the Child is defined as a standalone model. See #550.
Sorry for annoyance. It's still not clear to me.
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?
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 Car model to your app:
/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 setup() method instead of auto(magic) inheritance. What do you think about this particular use case?
@bajtos yeah that was the conclusion (more info)
I was testing it the other day and couldn't reach my use case, I think the very last child will need to recursively call their parents constructor, but from the base -> child direction, but that isnt the behavior
I need to do a little more investigation into this issue but yes I think we shouldn't do both inherit AND build.
Activity
violet-day commentedon Oct 25, 2014
I have the same question like u.But it's beforeCreate.Child will inherit Parent hook.
bajtos commentedon Oct 27, 2014
I am afraid this is a limitation of the current implementation of remoting hooks:
A workaround is to configure the hooks inside the
setupfunction, which is called whenever the model is extended./common/models/parent.js
[-]Inheriting Model and Remote Hooks[/-][+]Inheriting Remote Hooks[/+]rus0000 commentedon May 30, 2015
Seems same with a
remoteMethodas well. It only works if defined inside overloaded setup method in parent model.@bajtos Some questions about example:
/common/models/parent.js
1.
Parent.base.apply(this, arguments);- doesn't workParent.base.setup.apply(this, arguments);- worksWhy need to call
Parent.setup();if it isbajtos commentedon Jun 1, 2015
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
YourModelis created, loopback will callYourBase.setupand it's up to that method to callPersistedModel.setup.rus0000 commentedon Jun 1, 2015
In our example we have inheritance chain:
Model -> PersistedModel -> Parent -> ChildLoopback calls Child.setup, which is probably missing, therefore Parent.setup is invoked by searching prototype chain, as we have defined it with
Parent.setup = function() { ....In
Parent.setupwe call base class method withParent.base.setup.apply(this, arguments);which is defined inPersistedModel.setup. That's fine.But why there is a call of
Parent.setup();at the end of an example. Why to call itself immediately after definition?bajtos commentedon Jun 1, 2015
AFAIK, LoopBack does not call setup function when a new model is defined. In other words, if your
Childmodel provides asetupfunction, it is called only when somebody extends (inherits from) your Child, but not when the Child is defined as a standalone model. See #550.No worries, I am happy to explain. Is there anything else to explain in order to make this matter more clear?
rus0000 commentedon Jun 1, 2015
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?
bajtos commentedon Jun 1, 2015
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
Carmodel to your app:bajtos commentedon May 24, 2016
/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
setup()method instead of auto(magic) inheritance. What do you think about this particular use case?davidcheung commentedon May 24, 2016
@bajtos yeah that was the conclusion (more info)
I was testing it the other day and couldn't reach my use case, I think the very last child will need to recursively call their parents constructor, but from the base -> child direction, but that isnt the behavior
I need to do a little more investigation into this issue but yes I think we shouldn't do both inherit AND build.
bajtos commentedon Nov 12, 2019
LoopBack version 3 is in LTS, we won't be making breaking changes like this one.
itsdun1 commentedon Mar 3, 2020
does setup() inheritence also works with afterRemoteError()