Tree Component
Now that we have the selectors defined, the next step is to pass them into our component(s) so they can be used.
Each of our routes have a smart component that looks basically the same. So, once again, we'll look at the Tree (Standard)
page to illustrate how we use the selectors and how we only retrieve the data we need as it is needed.
Smart Component
At the route level, we create a smart component that retrieves the data from the selectors that we need.
The three selectors we need to pull our data from are, selectLocations()
, selectCurrentLocationId()
, and selectCurrentLocation()
. The code is as you would expect and can be found in the
You can look in the ngOnInit()
method to see that we retrieve the data and set them to member variables.
Next, we TreeComponent
locations
and locationId
are used to populate the dropdown that allows us to pick the current location. The location
is used to retrieve the data for the tree. Let's take a look at that next. The dropdown selector is relatively straightforward, so there is no real need to look at that in detail.
Dumb Component
The common
applyRange()
is responsible for kicking things off by determining which rows of information we need to display. It then calls transform()
to convert the data from locations
into a flat list of tree data.
Converting the nested data into a flat list is standard mat-tree
code so, again, we won't spend a lot of time on this. However, what is interesting about this code is that we avoid triggering retrieval of the data by not access the array elements unless we need the data. If all we need is the ID, we use the
And this is the magic of SmartNgRX. Once you retrieve the data directly from the element, SmartNgRX ends up calling the Effects Service you registered and retrieves the data from the server.
You'll also note that the transform()
method is