angular2-architecture
Strategie
Utilisation des services comme singleton,
- les proprietes sont des objets du domaine
Pb de loading et de rafraichissement
avantages de la subscription, mais parfois undefined….
Mais peux etre interessant, par exemple pour stocker une donnée qui ne change pas dans l’application
Ex : Login et User,
Utilisation des services avec des Observables
Utilisation de Redux et d’un Store Central
Les services servent alors d’utilitaires pour manipuler les données vennant du store
Le composant récupere les données du store, peut demander au service de filter, trier…. avant d’afficher les données ou de passer les données aux composant enfants
The services are singleton objects that
play two roles:
- Provide streams of data that our application can subscribe to
- Provide operations to add or modify data
• The services maintain streams which emit models (e.g. Messages)
• The components subscribe to those streams and render according to the most recent values
The point of the UserService is to provide a place where our application can learn about the current
user and also notify the rest of the application if the current user changes.
One consequence of streams is that, because messages are published immediately, a new subscriber
risks missing the latest value of the stream. BehaviourSubject compensates for this.
BehaviourSubject⁷⁴ has a special property in that it stores the last value. Meaning that any
subscriber to the stream will receive the latest value. This is great for us because it means that
any part of our application can subscribe to the UserService.currentUser stream and immediately
know who the current user is.
MessagesService: 3 “data management” streams and 2 “action” streams.
The three data management streams are:
• newMessages - emits each new Message only once
• messages - emits an array of the current Messages
• updates - performs operations on messages
map
filter
reduce