Best practice for using Subscribe and angular components?


#1

Currently i get stuck with the way to use subscribe in my components.

Currently i have a product page with have consists of multiple components that subscribe to the same product id.
What should i design for this case?

  1. Parse the id into components and let them subscribe to the same product id
  2. Parse the product object into the components

#2

Parent component that subscribes to the product, and send the id as a binding to the sub components, then each component can query (findOne) the collection for the data. Alternatively, you can send the whole object (or relevant part of it) as the binding to the sub component.


#3

I like to handle all my subscriptions in “smart components” (usually only one of these per route), then either pass the data down directly to the “dumb components” or pass the relevant info for them to query the data in minimongo (e.g. the _id).


#4

Put your subscribe part in the Service, then use Dependency Injection and subscribe in the parent component / smart component / container (whatever you call it).
Try to avoid putting them in the Component directly. Because these part are “API”, which related with the database.
It can help the code Testable and Reusable.
Check Subscribe data in Service using RxJS 5 .

If you are using ngrx(Redux), put the subscribe part in your Effects file. It will update the state in the Store through Reducer.
Check Reactive Programming (RxJS 5 Observable) and Redux (Store, Saga) in Angular2-Meteor