Created
January 23, 2021 02:47
-
-
Save rrcook/5a262c768a330f8d374dcef7b7a50da1 to your computer and use it in GitHub Desktop.
Real-world use of JavaScript Symbols and class expressions in Lightning Web Components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code from Lightning Web Components source, taken from Chrome dev console | |
const Navigate = Symbol("Navigate"); | |
const GenerateUrl = Symbol("GenerateUrl"); | |
const NavigationMixin = Base => { | |
assertIsLightningElementSubclass(Base); | |
return class extends Base { | |
[Navigate](pageReference, replace) { | |
navigationService.getService(this).navigateTo(pageReference, { | |
replace | |
}); | |
} | |
[GenerateUrl](pageReference) { | |
return navigationService.getService(this).generateUrl(pageReference); | |
} | |
}; | |
}; | |
NavigationMixin.Navigate = Navigate; | |
NavigationMixin.GenerateUrl = GenerateUrl; | |
// ... | |
exports.NavigationMixin = NavigationMixin; | |
// Used in client Code | |
export default class BoatDetailTabs extends NavigationMixin(LightningElement) { | |
// ... | |
// Navigates to record page | |
navigateToRecordViewPage() { | |
event.preventDefault(); | |
this[NavigationMixin.Navigate]({ | |
type: 'standard__recordPage', | |
attributes: { | |
recordId: this.boatId, | |
objectApiName: 'Boat__c', | |
actionName: 'view' | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment