Hosted By

Contact Me

Misc Links

OpenNTF BlogSphere LotusGeek CoComment Custom Button

Ads by Google

Welcome to keithstric.com!

I hope you find this site useful in some way or another. I strive to bring you all sorts of geeky information and solutions to your most frustrating of issues with the occasional rant on whatever topic, technical reviews and weblog. You'll also find many products that I've developed and make available for you to use however you like. So, grab a cup of coffee, sit down and visit for a while.

« Dojo, XPages and Graphics, oh My.... | Home Page| SnTT: XPages Calendar Control (Part 5), adding data to the day view... »

More XPages onclick event weirdness...

03/11/2010 11:52 AM By Keith Strickland

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

ID: 1
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!!

ID: 2
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. Emoticon

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.

ID: 3
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 Emoticon

ID: 4
Date: 03/11/2010 08:26:41 PM
Name: Keith Strickland
Website: http://www.keithstric.com

Ah! That explains it perfectly. Thanks Jeremy

ID: 5
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 }

Post A Comment

:-D:-o:-p:-(:-):-\:-|:angry::cool::cry::dontknow::emb::hairout::laugh::rolleyes::whew:;-)

Subscribe to keithstric.com

OpenNTF

Disclaimer

The opinions and ideas posted on keithstric.com are not necessarily the opinions and ideas of my employer. The solutions, techniques and code provided here are not guaranteed or warranted in any way and are free for you to use at your own risk.