Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Wednesday, 27 May 2009

Javascript plus and minus operator gotchas

So I am using the joy that is jquery to ajaxify our web application, and we have a calendar component.

The component has a "next" and "previous" which lets you move between weeks. So I had the following code:
/**
 * Calendar functions
 */
function addEventHandlersToCalendarComponent() {
    $('#previousWeekButton').click(function() {
        $("#calendarViewInMs").val($('#calendarViewInMs').val() - MILLISECONDS_IN_A_WEEK);
        refreshCalendar();
        return false;
    });

    $('#nextWeekButton').click(function() {
       $("#calendarViewInMs").val($('#calendarViewInMs').val() + MILLISECONDS_IN_A_WEEK);

        refreshCalendar();
        return false;
    });

    $('#calendarDateGoButton').click(function() {
        refreshCalendar();
        return false;
    });
}
The previous week worked perfectly, but clicking the next week broke with the server saying it couldn't parse the number.

Hmm.

After thinking about, and seeing the actual value being sent to the server, it was obvious: "-" operators convert both operands into a number, the "+" operator however, does no such goodness, it simple does a string concatenation. The solution was simple, to use the parseInt to coerce the result of the "val()" method into an actual int.

Thursday, 14 May 2009

Our latest web application can run in an environment that allows javascript, which means we can make our application much more dynamic.

Our UI consultant decided to use JSON and build up the UI dynamically. Other than the initial html page, the server doesn't return any other HTML.

This has a number of pros and cons, which I will blog about at a later date, but for now, it is off to learn the magic that is jQuery. This is almost certainly going to remind me once more why I always define myself as a "server side web developer" ;)