Skip to content

Instantly share code, notes, and snippets.

@rrcook
Created January 23, 2021 02:47
Show Gist options
  • Save rrcook/5a262c768a330f8d374dcef7b7a50da1 to your computer and use it in GitHub Desktop.
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
// 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