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
It would be nice if the afterLoad hook took a callback like the before hooks. For example if you need to retrieve something else from the database and add it to the object.
Thanks! Another thing that would be handy would be an afterFetch hook that gets triggered after related models have been autoFetched. If you like I can make an attempt to add these and create a pull request
This one wasn't easy, found 2 (not related to this) edge cases. Please test it. I'm not sure it's 100% working but it seems so. About afterFetch, you mean when autoFetch = true and after getting associations?
Whoa, that was fast. Thanks! I'll test it out this morning.
With respect to an afterFetch hook, yes, I found that when I tried to access associated models in afterLoad they were null.
For example I have tables called Channel and User with a many-to-many relationship between them. I want to query for messages in all of the channels Fred is subscribed to. To make this query perform faster, when I load a User, I want to get the ids of all the channels that user is in and set that as a property on the User object, so that I can write a query like this:
Here's an update. I called it afterAutoFetch to keep consistency with autoFetch option. Please remember this hook does not mean there are any associations, it just means that if exist, they were fetched.
Hi Diego,
I was thinking about this a bit more. When there are associated models with an auto fetch, I think it probably makes sense to wait until the associated models have been auto-fetched before calling the callback on find.
For example
Channel.find(3, function() { console.log(this.users) // at the moment this is null });
Activity
dresende commentedon Jul 5, 2013
I see, I'll see what we can do.
dirkmc commentedon Jul 5, 2013
Thanks! Another thing that would be handy would be an afterFetch hook that gets triggered after related models have been autoFetched. If you like I can make an attempt to add these and create a pull request
Adds ability to have the afterLoad hook blocking (#219)
dresende commentedon Jul 5, 2013
This one wasn't easy, found 2 (not related to this) edge cases. Please test it. I'm not sure it's 100% working but it seems so. About
afterFetch, you mean whenautoFetch = trueand after getting associations?dirkmc commentedon Jul 5, 2013
Whoa, that was fast. Thanks! I'll test it out this morning.
With respect to an afterFetch hook, yes, I found that when I tried to access associated models in afterLoad they were null.
For example I have tables called Channel and User with a many-to-many relationship between them. I want to query for messages in all of the channels Fred is subscribed to. To make this query perform faster, when I load a User, I want to get the ids of all the channels that user is in and set that as a property on the User object, so that I can write a query like this:
dirkmc commentedon Jul 5, 2013
afterLoad works well, thank you!
Adds hook afterAutoFetch triggered after extending and auto fetching …
dresende commentedon Jul 5, 2013
Here's an update. I called it afterAutoFetch to keep consistency with autoFetch option. Please remember this hook does not mean there are any associations, it just means that if exist, they were fetched.
dirkmc commentedon Jul 6, 2013
Wow, thanks so much for your fast work on this, I really appreciate it!
I have a couple of documentation suggestions:
It would be great if this were in the documentation in the many to many section.
Muito obrigado diego!
dresende commentedon Jul 6, 2013
Thanks.
Yes, the documentation needs some affection. About the hooks part, the
nextparameter is actually optional. It works (non blocking) without it :)dirkmc commentedon Jul 13, 2013
Hi Diego,
I was thinking about this a bit more. When there are associated models with an auto fetch, I think it probably makes sense to wait until the associated models have been auto-fetched before calling the callback on find.
For example
Does that make sense to you?
dresende commentedon Jul 14, 2013
That should be what is happening. If not I have to take a look at it.
dirkmc commentedon Jul 16, 2013
I haven't been able to reproduce this bug since I reported it. You can go ahead and close it and I'll reopen if I can create a reproducible test case.
dresende commentedon Jul 16, 2013
Ok :)