As a newcomer to Angular2, I am diving into hands-on learning. My current project involves building multiple views with parent components, child components, and database services. After successfully creating one view, I am now gearing up to implement others.
In order to ensure that all views utilize the same dataset - allowing for additions, updates, and deletions from any component - I am exploring the idea of incorporating a separate data layer. This layer would be accessible by all components in the application, eliminating the need for redundant trips to the database. How can I best define and utilize such a class in Angular2?
Update Q: With direct access to variables within the shared data layer throughout the application, what is the optimal approach for managing these variables within components?
a) Should I work with local component variables that mirror those in the data layer (thus loading, retrieving, and updating them explicitly), like
this.locations = this.datalayer.locations;
this.selectedLocation;
updateLocation(id) {
this.selectedLocation = id;
this.datalayer.setSelectedLocation(id);
}
getSelectedLocation() {
return this.selectedLocation;
}
or b) should I solely interact with data layer variables, accessing and modifying them directly within a component?
updateLocation(id) {
this.datalayer.selectedLocation = id;
}
getSelectedLocation() {
return this.datalayer.selectedLocation;
}
Could there be an alternative option c?