Hi, so this is in the context of a bookmarksPage assigned to a tab.
The data in my bookmarks array of objects can be listed flat “List All”
or “By Chapter” or “By Date” … as shown in the snapshot below.
The bookmarksPage template (html) and code started to get too large to manage so I decided to factor out chunks of code and html into what seemed logical child components
ListAll.ts/html, GroupedByChapter.ts/html, GroupedByDate.ts/html and so on.
All of these child components as well as the parent bookmarksPage inject BookmarksService.ts to access the master bookmarks data.
The filter buttons are hosted at the bookmarksPage and I wanted to control all such child components via some interface(s) they implement.
The interfaces after getting connected to the correct child component instance would be used to perform common things across all of them like SortUp, SortDown, Expand, Collapse, Restore etc…
However when I try to reconnect the interfaces to the non active (hidden) child components (and because of me using *ngIf) I am observing they are still undefined This happen when I try to re-execute the following routine based on the selected view.
/**
* Connect the needed interfaces
* - 20200228 */
ReconnectInterfaces() {
//
if (this.ViewMode === ViewModeValues.ByNone)
this.CollapseExpand_I = undefined // INOTE: not yet
if (this.ViewMode === ViewModeValues.ByType) {
this.CollapseExpand_I = this.GroupedByType
}
if (this.ViewMode === ViewModeValues.ByDate) {
this.CollapseExpand_I = this.GroupedByDate
}
if (this.ViewMode === ViewModeValues.ByChapter) {
this.CollapseExpand_I = this.GroupedByChapter
}
}
I will add more interfaces above as I go. The instances to the child components I obtained via @ViewChild are evaluating to undefined because they are getting recreated.
One of the benefit of using an interface is that the code behind the buttons like in the following collapse/expand would be simplified to using the interface if one exists and avoids using if-then-else(s) (aka switch statement)
like so:
CollapseAll() {
this.IsAllCollapsed_ = true
if (this.CollapseExpand_I !== undefined)
this.CollapseExpand_I.Collapse()
}
ExpandAll() {
this.IsAllCollapsed_ = false
if (this.CollapseExpand_I !== undefined)
this.CollapseExpand_I.Expand()
}
SortUp(){ ... }
SortDown(){ ... }
This is how I code it in the .NET world except here in this app I am dealing with Angular/Ionic ever changing implementations of Page/Components Lifecycles, which I can’t possibly keep abreast of nor their impact.
I am happy how the specifics to each of the views is localized in the corresponding child component code and template.
However, after factoring out the child components I realized, a bit too late, that ionic/angular does NOT extend the lifecycle hooks into inner components.
I don’t believe I have a case here for using @input @output (nothing to hand-in or out).
Hopefully I have explained enough and the snapshot is helpful.
I am open to doing it differently per your suggestion. FYI, I take whatever you post and other contributors like a black box and I keep apply it whether it be observables, promises, etc… until it breaks and then have to come back here for help No matter, how hard I try to read I forget and it remains foreign to me. By the time the summer comes I am out hunting and that super clears all my memory blank and I even forget there was a CLI and that I need to start Gitbash Then 3-4 months later around December I may come back into ionic programming and tell myself is this the last time I am going to touch code, the same is true with .NET, …old age
Today, I tried to move from ionic 4.0 to 5 and that was OK but everything went haywire after upgrading to Angular 9 following to the migration to 5.0 video.
So I worked hard recovering a running state of this app.
Let me know if you need more clarifications.
Thanks a bunch for all your insights.