I need help incorporating a global object in TypeScript for my application. Specifically, I want to have user details available and bindable throughout the entire application after a remote call. Can someone provide an example of how this can be achieved?
I am currently working on implementing a singleton class for the user details.
User Details:
export class UserDetails {
//Populated for all remote calls
userGuid: string;
firstName: string;
lastName: string;
fullName: string;
isAuthenticated: boolean;
private static _instance: UserDetails = new UserDetails();
constructor() {
if (UserDetails._instance) {
throw new Error("Error: Instantiation failed: Use UserDetails.User instead of new.");
}
UserDetails._instance = this;
}
public static get User(): UserDetails {
return UserDetails._instance;
}
@computedFrom('fullName')
get FullName(): string {
return this.fullName;
}
}
Another Page:
import {UserDetails} from 'models/userDetails';
export class Home {
/*@bindable*/router: Router;
//Currently does not contain the values
//that are populated after every remote call to the server
userDetails = UserDetails.User;
}
Thank you in advance, Senthil S