best way to inject pass parameters to Main? js

101 views
Skip to first unread message

TroyWorks

unread,
Oct 5, 2014, 11:19:13 PM10/5/14
to haxe...@googlegroups.com
I  have a JS single page app, wondering the best pass data from outside the app into the haxe/js output.

In the index.html it has this

    <script type="text/javascript"> var initData = "abcd...."; </script>
    <script type="text/javascript" src="js/app.js"></script>

where the initData is what I need to pass in to the haxe that's like this:

class Main  {

   
static function main(){
//do stuff, that needs initData.
   
}
}

I've tried making main do nothing and adding a second static function, which I can call like

    <script type="text/javascript">
       
Main.startup("passed in");
   
</script>



 but then there is a race issue between when the app is loaded and read and when the main is called..

Waiting for the DOM onLoad is one way but also adds a delay, I'd like to avoid.

Any ideas?

Dan Korostelev

unread,
Oct 6, 2014, 2:52:06 AM10/6/14
to haxe...@googlegroups.com
Compile without -main, by simply specifying the entry class, then call its exposed startup function. I.e.

class Main {
    @:expose
    static function startup(data:String) {...}
}

and then haxe -js your.js Main

понедельник, 6 октября 2014 г., 7:19:13 UTC+4 пользователь TroyWorks написал:

TroyWorks

unread,
Oct 10, 2014, 6:50:13 PM10/10/14
to haxe...@googlegroups.com
I ended up having the main function reach out to the dom, get a xml node by id , that is xml of all the configuration. e.g.

<body><div>hello there</div><xml id="config"><name>Bob</name></xml></body>

Where the xml isn't visualized by the browser.

This was greatly helpful, shaved about half a second in perceived loading time, vs the old way of the javascript using http request to load up the xml once everything else had loaded.

Rafael Oliveira

unread,
Oct 10, 2014, 7:35:32 PM10/10/14
to haxe...@googlegroups.com
This is new to me, I didn't know that we can put xml inside of html.

Pier Bover

unread,
Oct 10, 2014, 8:37:31 PM10/10/14
to haxe...@googlegroups.com
It's called xhtml.

On Fri, Oct 10, 2014 at 6:35 PM, Rafael Oliveira <rafa...@gmail.com> wrote:
This is new to me, I didn't know that we can put xml inside of html.

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
Pier Bover
y...@pierbover.com

Daniel Glazman

unread,
Oct 10, 2014, 9:03:12 PM10/10/14
to haxe...@googlegroups.com
On 11/10/2014 02:37, Pier Bover wrote:

>>> <body><div>hello there</div><xml id="config"><name>Bob</name></xml></body>
>>
>> This is new to me, I didn't know that we can put xml inside of html.
>
> It's called xhtml.

Ahem. The xml element above is not XML and this is not necessarily
a XHTML document.

The fragment above, alone, is a HTML document in quirks mode and
unknown HTML elements are rendered with a 'display: inline' default
style. To hide it in the rendered view, you need to switch that to
'display: none'.
Now I suppose (and I hope) TroyWorks' document is not a plain quirks
mode document; it's more probably a html5 document these days.
But that does not change the result: the xml element above is an
unknown HTML element. You can verify that in JavaScript through:

document.getElementById("config") instanceof HTMLElement

that should be true.

</Daniel>

TroyWorks

unread,
Oct 10, 2014, 11:47:48 PM10/10/14
to haxe...@googlegroups.com
Here's how it's actually being done:


<script id="quizXML" type="text/xml"><trivia >
    <settings
        showReadyScreen="true"
        autoGoToNext="false"

or

<xml id="quizXML" type="text/xml"><trivia >
    <settings
        showReadyScreen="true"
        autoGoToNext="false"


      var xml =  js.Browser.document.getElementById("quizXML").innerHTML;

Philippe Elsass

unread,
Oct 11, 2014, 2:29:11 AM10/11/14
to haxe...@googlegroups.com

You can also define a script tag before your Haxe JS to define a startup payload:
<script>
var config = {
    foo:"bar"
}
</script>
<script src="index.js"></script>

And in Haxe:
var config = untyped window.config;

Daniel Glazman

unread,
Oct 11, 2014, 3:10:56 AM10/11/14
to haxe...@googlegroups.com
On 11/10/2014 05:47, TroyWorks wrote:

> var xml = js.Browser.document.getElementById("quizXML").innerHTML;

Exactly what I said. innerHTML:-) Would not work on a non-HTML
element... BTW, I really wonder why you use innerHTML and not
direct access to elements, attributes and the textContent property
on nodes. That's considerably more reliable.

</Daniel>

TroyWorks

unread,
Oct 12, 2014, 1:43:07 AM10/12/14
to haxe...@googlegroups.com

On Friday, October 10, 2014 11:29:11 PM UTC-7, Philippe Elsass wrote:

You can also define a script tag before your Haxe JS to define a startup payload: <script>
var config = {
    foo:"bar"
}
</script>


That's JSON though. In this case I have to consume configuration xml produced by another external tool, I'm embedded into the html solely to speed up loading times..vs loading it after the app has initialized.

That said I'm sure it could be passed as a var xml= "XML STRING" and/or converted to json in a build step.

Reply all
Reply to author
Forward
0 new messages