More XPages onclick event weirdness...
I posted before about some weirdness with the onClick event code firing at page load time. I determined that if you edit the onclick event from the All Properties tab that you'll get this kind of behaviour. Well, here's another one.
The scenario, on the XPages Calendar, Day view I've got the normal navigation links at the top of the page to navigate forward/back a day. I'm using the dojo.gfx package to place day entries on the page (and making some decent headway by the way). On the day view page (ccCalDay) I've included a client side javascript library that contains the function which gathers data embedded on the page (Thanks Jeremy) and then places the dojo.gfx objects where they should go. Now, in the function which gets called for the dojo.addOnLoad event I get a node list of all the "calEntry" tags and their children using dojo.query. I then loop through each node and grab the "value" listed for that node. Simple enough, however the dojo.query call breaks the onclick of my navigation links.
Here's the code I placed in the Source of my ccCalDay custom control:
Now, for the function I just had:
Now, I know you're saying I must be doing something strange for this to be happening. Maybe I am, but I don't think I am, the code really isn't that complex. I set quite a few variables, build the gfx objects and that's it. I'm posting this because to me it's starting to show a pattern that the onclick event is flakey (that's a technical term by the way) at best. At worst it creates time consuming problems that don't make sense and are difficult to troubleshoot. While I know XPages are just starting to come into their own and are young but shouldn't these types of issues have already been found and addressed somewhere? Surely IBM and a lot of the business partners that get to see this stuff before it's released have run into some of these same types of issues? I say this because they build far more complex applications than myself that are tested and tested some more.
But here's how I got around this frustrating, time sucking issue. I went back to Julian Buss's XPages Wiki and this article to back track a little bit and put the addOnLoad back to how I originally had it and then added the calEntries call here instead of in the function. Here's what I ended up with in the Source of the ccCalDay custom control:
And the function now contains:
This seems to have corrected the problem but man what a headache. I'm not sure if this happened because of my inexperience with dojo and javascript or what. But I still think that the onclick event of links needs to be inspected a little bit more at IBM.











Comments
Date: 03/11/2010 03:35:46 PM
Name: Jeremy Hodge
Website: http://www.hodgebloge.com
Ok, here is what is going on, its a bit confusing, but its doing what you told it to....
The problem is in the line:
dojo.addOnLoad(addOnLoadFunc());
more specifically, the addOnLoadFunc()
and even more specifically the () ...
Because you've added the () to the end of the function, when the browser runs the line dojo.addOnLoad(...) it immediately executes addOnLoadFunc() and then tries to take the results and pass it to addOnLoad. What you really want to do is pass just addOnLoadFunc which is an identifier for the function, and then addOnLoad will register that function to run on load. So the correct call is:
dojo.addOnLoad(addOnLoadFunc);
or
dojo.addOnLoad(function() { addOnLoadFunc() })
which would create an anonymous function passed to addOnLoad that then executes addOnLoadFunc() when it gets called.
Hope that helps explain it!!
Date: 03/11/2010 04:51:44 PM
Name: Keith Strickland
Website: http://www.keithstric.com
Thanks Jeremy. I'm still not quite sure I understand but it kinda makes sense.
I guess what I gather from this is it's expecting an anonymous function not really a function call. I guess I need to read some more on that dojo.addOnLoad().
It's kind-of comforting that this is because of my inexperience rather than with XPages itself. I can accept that better than an issue with the product.
Date: 03/11/2010 05:59:42 PM
Name: Jeremy Hodge
Website: http://www.hodgebloge.com
Hey Keith ...
Its not so much that it needs the anonymous function, its that you have to pass the actual function itself to addOnLoad. When you put the () after the function name, you are telling javascript to execute the function immediately.
The disconnect I think is you need to think of "addOnLoadFunc" as a variable that represents your function, and "addOnLoadFunc()" as actually calling the function ... what dojo.addOnLoad needs is the variable that represents your function, so it knows later what to run when dojo has finished loading.
Hope that makes more sense than how i tried to explain it last time
Date: 03/11/2010 08:26:41 PM
Name: Keith Strickland
Website: http://www.keithstric.com
Ah! That explains it perfectly. Thanks Jeremy
Date: 03/18/2010 06:58:31 AM
Name: Sean Cull
Website: http://www.seancull.co.uk
Keith, thanks for this, you have preserved my sanity !
I thought there were ghosts in the machine.
{ Link }