Having Collection helper wait until subscription ready

Hi!

I have two separate templates that display information about a certain collection. There’s common functionality that then changes the UI in their respective templates. I thought it’d be good to abstract this functionality away into the Collection itself as a Helper.

Problem is, this helper relies on data from other collections too. So, I have one template that makes a call to this collection helper but it fails because it’s missing references to other data. Seems like it’s trying to render the DOM before the subscription has finished…? I added an if to check for if the subscription is ready first, and that seems to work. Problem is, it introduces a flicker.

How can I handle situations like this where different templates want to use the same collection helper? But with regards to waiting until all data has loaded and without flicker?

Subscribing to data should be relegated to your view layer since that is what is going to be displaying the information and thus is what is affected. Subscribing to and waiting for data from within a collection helper seems like a pattern that will cause you many headaches.

If you have collection data that relies on data in other collections, it is probably best to publish it all together using something like publish-composite or grapher, so that you can wait for the subscription to be ready within your view layer, and then display everything once you have all the data on the client.

1 Like

“Subscribing to and waiting for data from within a collection helper seems like a pattern that will cause you many headaches.”

Haha, I thought I was being clever. Yeah, it’s not so much as me having data from 2 separate collections but rather 2 views needing to use same code (and so I abstracted away to collection helper).

I guess my solution would be to abstract this elsewhere to some kind of helper class or consider restructuring MVC in app