I am trying to use a global object in TypeScript. For example, when a remote call is done, the user details gets populated. I would like it to be available throughout the application and be bindable. Can someone point to an example how it can be done?
I am implementing this type of singleton class for the user details class.
User Details:
export class UserDetails {
//Gets populated for all the 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;
}
}
Other Page:
import {UserDetails} from 'models/userDetails';
export class Home {
/*@bindable*/router: Router;
//This does not have the values
//that gets populated after every remote call to the server
userDetails = UserDetails.User;
}
Thanks in advance Senthil S