Any component that needs to use the mechanism needs to tell it when to be triggered and when not to.
example.component.ts
...// import PendingChangesComponent from @valtimoimport { PendingChangesComponent } from'@valtimo/components';...//import modalService and translateService for the Modal to workimport {ModalService} from'carbon-components-angular';import {TranslateService} from'@ngx-translate/core';......// Make the component targetable by the PendingChanges guardexportclassExampleComponentextendsPendingChangesComponent {...// initialize PendingChangesComponentconstructor(private modalService:ModalService,private translateService:TranslateService ) {super(modalService, translateService) }......// let the component know the guard needs to be triggeredpublicexampleDoChange():void { //pendingChanges is an attribute of the PendingChangesComponent that lets it know the guard might need to be triggered
this.pendingChanges =true;... }...// let the component know the changes have been savedpublicexampleSaveChanges():void {...this.pendingChanges =false; }...}
Custom handlers when closing the PendingChanges modal
In case custom handlers need to be added when selecting either Confirm or Cancel, the PendingChangesComponent has two protected methods that can be overwritten for that purpose.
When overwriting these method, the logic inside of them gets triggered right before closing the modal.
example.component.ts
...// import PendingChangesComponent from @valtimoimport { PendingChangesComponent } from'@valtimo/components';...//import modalService and translateService for the Modal to workimport {ModalService} from'carbon-components-angular';import {TranslateService} from'@ngx-translate/core';......// Make the component targetable by the PendingChanges guardexportclassExampleComponentextendsPendingChangesComponent {...// initialize PendingChangesComponentconstructor(private modalService:ModalService,private translateService:TranslateService ) {super(modalService, translateService) }......// overwrite onCancelRedirect methodprotectedonCancelRedirect():void {//custom logic for canceling of navigation }...}
Configuring a custom pending changes modal
In case a different modal needs to be displayed instead of the default PendingChanges confirmation, this can be achieved through the following property and by making use of the following methods.
In order to set the custom modal, your component needs to be implemented like this:
example.component.ts
...// import AfterViewInit and ViewChild to load your custom modalimport { AfterViewInit, ViewChild } from'@angular/core';// import PendingChangesComponent from @valtimoimport { PendingChangesComponent } from'@valtimo/components';...// Make the component targetable by the PendingChanges guardexportclassExampleComponentextendsPendingChangesComponentimplementsAfterViewInit {...// CustomModalComponent is the modal you'd like to display instead of the default pending-changes modal @ViewChild(CustomModalComponent) modalComponent;...// assign your custom modalpublicngAfterViewInit():void {// customModal field is protected from the PendingChangesComponentthis.customModal =this.modalComponent }...// method in your component that is called when redirecting is confirmedpubliconConfirmClick():void {// method from the PendingChangesComponent in order to let the guard know that the redirect can happenthis.onCustomConfirm(); }// method in your component that is called when redirecting is canceledpubliconCancelClick():void {// method from the PendingChangesComponent in order to let the guard know that the redirect is not allowedthis.onCustomCancel(); }...}