Welcome
Welcome to saltarellecompiler

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

How to define program entry point?

This is where you ask quesions about how to use the compiler.

How to define program entry point?

Postby ashraf_sabry_m » Mon Sep 24, 2012 5:03 pm

I'm wondering where and how to define a Saltarelle program entry point? I mean global code that's executed after everything is initialized.
In plain JS, I write this code at the bottom of the last included JS file, or in the page after including all the files.
In Win32 applications, a static Main method is considered the entry point of the application.

In the example code in your "getting started page", you used a static constructor, but this doesn't guarantee that this code is executed after all the types are initialized.
ashraf_sabry_m
 
Posts: 19
Joined: Sun Sep 23, 2012 3:26 pm

Re: How to define program entry point?

Postby erik-kallen » Mon Sep 24, 2012 6:09 pm

The static constructor will be run after all types in its assembly has been initialized. Also, in the getting started guide, I'm using jQuery.OnDocumentReady() to surround the code to be run, which means it is deferred until the page has been loaded (it is translated to $(function() { /* whatever */ }))
erik-kallen
Site Admin
 
Posts: 78
Joined: Fri Sep 21, 2012 12:09 pm

Re: How to define program entry point?

Postby ashraf_sabry_m » Mon Sep 24, 2012 7:09 pm

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.
ashraf_sabry_m
 
Posts: 19
Joined: Sun Sep 23, 2012 3:26 pm


Return to Support Questions

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred