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.

« SnTT: XPages Calendar Control (Part 3), making it reusable | Home Page| SnTT: XPages Calendar Control (Part 4), adding day and week views »

Weird SSJS Time/Date stuff...

02/23/2010 3:30 PM By Keith Strickland

While working on the Day view of the XPages Calendar I've kind-of re-arranged the way I'm calculating the different parts of the calendar. Instead of using separate variables for the Month and Year I've setup a date variable which holds a JavaScript date object and then use the getMonth() and getFullYear() functions of the date object. This makes it easier to navigate between the different days and months, you just change that one variable and everything else gets computed from that one date. However, I've come across some weirdness (for lack of a better term) with the getMonth() function that I can't for the life of me figure out.

OK, so I've setup an array with the names of all the months. This is a zero based array because with JavaScript the month numbers are zero based. So, here's what I've got:
sessionScope.put("months", new Array("January","Feburary","March","April","May","June","July","August","September","October","November","December"));
sessionScope.put("dispDate",new Date());
sessionScope.put("dispCalMonth", sessionScope.get("dispDate").getMonth());

Now, the dispCalMonth sessionScope variable contains the correct month, which for Feburary is 1 (remember zero based months). Farther down the form I've got a computed field that uses:
var curMonth = sessionScope.months(sessionScope.get("dispDate").getMonth());
return curMonth;

Now, this is returning 0 (January) instead of 1 (Feburary). I can't figure out why and it's driving me nuts I've even returned just the number instead of the month name, and it does return 0.

Another issue I'm running into is using the setMonth(), setDate() and setFullYear() functions. These don't seem to work at all, at least not on sessionScope variables. I am able to set a new Date variable and use these functions and set the dispDate sessionScope variable to the new date variable. Which while workable, it is rather frustrating

These simple little things are the biggest hold up on Part 4 of the XPages Calendar which will include the day view and hopefully an "Alpha" release to OpenNTF. So, gentle reader, if you've got any ideas on what's going on here, please chime in while I still have a few hairs left.

UPDATE:
OK, in front of the computed field that displays the month is a link. In the onClick event of the link I decrement the dispDate sessionScope variable month. I removed this link and now the month display in the computed field is correct. So, it seems the onClick code is affecting the computed field somehow, so maybe someone can explain that one to me?

UPDATE #2:
OK, after searching the 8.5 Forum I found this post which said he got rid of all the event code and added it back via the source code editor. Well, I tried this with no joy. So, out of desparation I removed all the event code from the link and went and looked at the source. There was still an xp:this.onclick statement in the source and I still experienced the same results I defined above. This tag should not have been where it was, it should have been within the xp:EventHandler and then xp:this.action tags of the source. So I believe this is the whole problem. Now that I know what to look for it should be easy to spot. Also, I think the xp:this.onclick tag gets added if you edit the onclick event from the All properties tab instead of the events section.


Comments

ID: 1
Date: 02/23/2010 06:30:49 PM
Name: Jeremy Hodge
Website: http://www.hodgebloge.com

var curMonth = sessionScope.months(sessionScope.get("dispDate").getMonth());

should be

var curMonth = sessionScope.months[sessionScope.get("dispDate").getMonth()];

You have used () instead of [] on the array to reference the index number ...

As for why the .setXXXX() functions aren't working, i'm guessing its because the results never get "put" back into the hash ... for example, you many need to do this:

sessionScope.put('dispDate', sessionScope.get('dispDate').setFullYear(2009));

Otherwise, you get the value, which is a copy of the object from the hash, you modify it, and then once you leave the scope of the current function, the value is lost because sessionScope was never updated...

ID: 2
Date: 02/23/2010 06:55:37 PM
Name: Keith Strickland
Website: http://www.keithstric.com

Thanks Jeremy! Yeah, I typed the array reference rather quickly and didn't really copy/paste. Guess that just goes to show not to be too hasty to get things out Emoticon

But I figured out the other thing. Seems if you edit the onClick event from the All Properties tab an extra tag gets put into the source. For the onclick event you should have an <xp:eventhandler> tag which defines the event, and then within that tag another tag called <xp:this.action>. Well, if you use the All Properties tab to edit the onclick event a tag gets put outside the event handler called <xp:this.onclick>. This is the problem. Remove that tag and all of it's code and all is well.

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.