I too have a $(document).ready in my project, but also I have some global initialization code, like creating Knockout binding handlers and JQ validation rules.
Currently I don't have problems in the project, but I can contrive the following:
I have a class called Main, which has a static instance of another class called Preloader.
Main has a static method called Initialize as follows:
- Code: Select all
public static class Main
{
...
public static readonly Preloader Preloader = new Preloader();
public static void Initialize()
{
...
Preloader.Show(OperationType.Code);
}
}
Initialize invokes Show on Preloader, and Main has a static constructor:
- Code: Select all
static Main()
{
Initialize();
}
Here's part of the emitted JS:
- Code: Select all
...
Application.Main.preloader = new Application.Preloader();
Application.Main.initialize();
...
Application.Preloader.$waitInterval = 500;
Here initialize will use the preloader before waitInterval is initialized which will cause a problem.
WaitInterval is declared as a private const, and is used directly inside Show.
What I think about is either following the Win32 convention and treat static Main as application entry point, and to be emitted as anonymous self executing function in JS. So this C#:
- Code: Select all
public static void Main()
{
// Code
}
Will emit this JS at the very bottom of the file:
- Code: Select all
(function()
{
// Code
})();
Or, if you don't like this convention, maybe a new attribute will do the job.