I am new to TypeScript and at the moment I am experiencing a strange problem. I instantiate my main class when the document is ready using jQuery.
var main: MainApp;
$(document).ready(function () {
main = new MainApp();
});
Simplified MainApp class:
class MainApp {
net: AppNetworking;
urlHelper: UrlHelper;
cat: Category;
constructor() {
this.net = new AppNetworking();
this.urlHelper = new UrlHelper();
}
private AjaxCallback(categoryData){
this.cat = new Category(categoryData);
}
static onClickSendButton(): void{
var hostUrl: string = main.urlHelper.getQueryStringParam("HostUrl");
if (main.cat.isValidCategory()) {
main.sendCategory();
}
}
sendCategory(): boolean {
}
}
The function is registered in the onClick event of the button when building the class MainApp.
$("#btnSendCat").click(MainApp.onClickSendButton);
When a function is called onClickSendButton(), it throws an error:
Uncaught TypeError: Cannot read property 'isValidCategory' of undefined
When debugging, urlHelperInstance is determined , but the instance catis undefined. Since I do not affect the instance catanywhere in the application, I am really confused that it is undefined. Also, when checking a variable, mainall members are defined!
Am I doing something illegal here? Could there be problems with this code?