Haxe/Js - onClick refuse to attach

79 views
Skip to first unread message

michael solomon

unread,
Jul 29, 2015, 6:37:05 PM7/29/15
to Haxe
I have a a problem that drive me crazy: Haxe/Js I do an ajax(Http) with haxe and then attach to the element that I get from the ajax an onclick event. If I don't alert before it's sometimes work and sometimes not (mostly not..). WTF??? here my code http://try.haxe.org/#ff0fe look for initRegisterPage function
Thanks :)
Miki

Marcelo de Moraes Serpa

unread,
Jul 29, 2015, 6:51:54 PM7/29/15
to haxe...@googlegroups.com
This sounds like a race condition. I haven't looked at the code, but make sure that you're binding the event IN the success callback of the Ajax call.

--
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.

Marcelo de Moraes Serpa

unread,
Jul 29, 2015, 6:53:37 PM7/29/15
to haxe...@googlegroups.com
Ajax requests are async by default (unless you tell it otherwise). If you assign the event outside of the callback, it could happen that the request haven't completed yet, by the time the code that binds the event is executed.

Marcelo de Moraes Serpa

unread,
Jul 29, 2015, 6:53:54 PM7/29/15
to haxe...@googlegroups.com
s/haven't/hasn't

michael solomon

unread,
Jul 30, 2015, 1:03:44 AM7/30/15
to Haxe
Thanks, but If you look on my code you'll see that I turn in onData event... This should do the work.. No?

Philippe Elsass

unread,
Jul 30, 2015, 4:53:05 PM7/30/15
to haxe...@googlegroups.com
What is wrong in your code is the for (btn in menu) loop: the onclick closures you create in the loop will all share the same btn variable (and value) which is defined/hoisted outside the loop. In short, all the buttons will use the last btn's href.

If you want to capture a specific btn value with the onclick closure you should move that into a function: for (btn in menu) setClickHandler(btn);

On Thu, Jul 30, 2015 at 6:03 AM, michael solomon <miche...@gmail.com> wrote:
Thanks, but If you look on my code you'll see that I turn in onData event... This should do the work..  No?
--
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.



--
Philippe

Philippe Elsass

unread,
Jul 30, 2015, 5:01:51 PM7/30/15
to haxe...@googlegroups.com
Ah sorry for the problem initRegisterPage it looks correct. Random guess: is "registerSumbit" a typo for "registerSubmit"? Or maybe you have several "registerSumbit" elements?

Also 'URLReq.onError("Can't load index");' immediately executes .onError - that's probably not the desired effect.
--
Philippe

michael solomon

unread,
Jul 30, 2015, 6:59:16 PM7/30/15
to Haxe, philipp...@gmail.com
Thanks,


> What is wrong in your code is the for (btn in menu) loop: the onclick closures you create in the loop will all share the same btn variable (and value) which is defined/hoisted outside the loop. In short, all the buttons will use > the last btn's href.

"btn.onclick" in "for (btn in menu) " 0_0 or that I missed something...


> Ah sorry for the problem initRegisterPage it looks correct. Random guess: is "registerSumbit" a typo for "registerSubmit"? Or maybe you have several "registerSumbit" elements?
Also 'URLReq.onError("Can't load index");' immediately executes .onError - that's probably not the desired effect.

Amm... no.. I think my code is right... I think it's something with the browser: if I put a break point before the "initRegisterPage() " it work fine, if I put the breakpoint after the "initRegisterPage() " the event not attach.
Also, if I put haxe.Timer(100) before it work fine...
It's looks like the browser not insert yet the content from the Ajax(c.item(0).innerHTML = content;) but what really weird is that if I do: "trace(doc.getElementById("registerSumbit"))" I get the dom of this element so it exist in DOM!!!
I so hate JS :( :( :@

Philippe Elsass

unread,
Jul 31, 2015, 4:40:09 AM7/31/15
to michael solomon, Haxe
On Thu, Jul 30, 2015 at 11:59 PM, michael solomon <miche...@gmail.com> wrote:
Thanks,

> What is wrong in your code is the for (btn in menu) loop: the onclick closures you create in the loop will all share the same btn variable (and value) which is defined/hoisted outside the loop. In short, all the buttons will use > the last btn's href.

"btn.onclick" in "for (btn in menu) " 0_0 or that I missed something...

I meant inside the closure: URLReq = new Http(btn.attributes.getNamedItem("href").value);
 


> Ah sorry for the problem initRegisterPage it looks correct. Random guess: is "registerSumbit" a typo for "registerSubmit"? Or maybe you have several "registerSumbit" elements?
Also 'URLReq.onError("Can't load index");' immediately executes .onError - that's probably not the desired effect.

Amm... no.. I think my code is right... I think it's something with the browser: if I put a break point before the "initRegisterPage() " it work fine, if I put the breakpoint after the "initRegisterPage() " the event not attach.
Also, if I put haxe.Timer(100) before it work fine...
It's looks like the browser not insert yet the content from the Ajax(c.item(0).innerHTML = content;) but what really weird is that if I do: "trace(doc.getElementById("registerSumbit"))" I get the dom of this element so it exist in DOM!!!
I so hate JS :( :( :@

That is strange because this should effectively be immediate - you may want to share a complete project. Also maybe just try to approach what you are trying to do differently if you're blocked by this issue. 

Philippe


On Friday, July 31, 2015 at 12:01:51 AM UTC+3, Philippe Elsass wrote:
Ah sorry for the problem initRegisterPage it looks correct. Random guess: is "registerSumbit" a typo for "registerSubmit"? Or maybe you have several "registerSumbit" elements?

Also 'URLReq.onError("Can't load index");' immediately executes .onError - that's probably not the desired effect.

On Thu, Jul 30, 2015 at 9:52 PM, Philippe Elsass <philipp...@gmail.com> wrote:
What is wrong in your code is the for (btn in menu) loop: the onclick closures you create in the loop will all share the same btn variable (and value) which is defined/hoisted outside the loop. In short, all the buttons will use the last btn's href.

If you want to capture a specific btn value with the onclick closure you should move that into a function: for (btn in menu) setClickHandler(btn);

On Thu, Jul 30, 2015 at 6:03 AM, michael solomon <miche...@gmail.com> wrote:
Thanks, but If you look on my code you'll see that I turn in onData event... This should do the work..  No?

--
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.



--
Philippe



--
Philippe



--
Philippe

Juraj Kirchheim

unread,
Jul 31, 2015, 4:52:21 AM7/31/15
to haxe...@googlegroups.com
On Fri, Jul 31, 2015 at 12:59 AM, michael solomon <miche...@gmail.com> wrote:
Thanks,

> What is wrong in your code is the for (btn in menu) loop: the onclick closures you create in the loop will all share the same btn variable (and value) which is defined/hoisted outside the loop. In short, all the buttons will use > the last btn's href.

"btn.onclick" in "for (btn in menu) " 0_0 or that I missed something...

Actually, Philippe is wrong on this one. Each iteration of a loop uses its own variable through self invoking anonymous functions, as Haxe does not share JavaScript's quirky semantics despite unyielding pressure to do so.

As for the actual problem at hand, it would really help to have a minimal working example.

Best,
Juraj

Philippe Elsass

unread,
Jul 31, 2015, 1:26:19 PM7/31/15
to haxe...@googlegroups.com
Oh oh I stand corrected - that's some clever JS generation we've got here :)

I'm spending my time inspecting the generated JS but never noticed this pattern; I probably never write code that triggers this workaround...


   

--
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.



--
Philippe

michael solomon

unread,
Aug 1, 2015, 7:17:17 PM8/1/15
to Haxe
Oh. I did a rookie mistake... I migrate an old JS project and forgot to translate one of the files... so it keep running and screw up everything...
Thanks a lot guys! that's was very helpful!
Reply all
Reply to author
Forward
0 new messages