Kris Schouw
Kris Schouw

Reputation: 352

jQuery FullCalendar Event Function Callback is not Rendering My Events

I'm using the jQuery FullCalendar plug-in for a project I'm working on, and I'm attempting to render events within the calendar that are returned from a request to a Java servlet. The servlet is to return an XML document which I then parse to build an event before pushing it into the necessary array that I then pass to the callback function.

Code

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="fullcalendar.css" />
        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript" src="fullcalendar.min.js"></script>
        <script type="text/javascript">
            $(document.ready(function() {
                $("#calendar").fullCalendar({
                    events: function(start, end, callback) {
                        $.get("servlet?command=getEvents", function(data) {
                            var events = [];
                            $(data).find("event").each(function() {
                                events.push({
                                    id: $(this).find("id").text(),
                                    title: $(this).find("title").text(),
                                    start: $(this).find("start").text(),
                                    end: $(this).find("end").text(),
                                    url: $(this).find("url").text()
                                });
                            }
                            callback(events);
                        }, "xml");
                    }
                });
            });
        </script>
    </head>
    <body>
        <div id="calendar"></div>
    </body>
</html>

XML Returned From Servlet

<xml>
    <event>
        <id>Event1</id>
        <title>Title1</title>
        <start>2011-10-18</start>
        <end>2011-10-19</end>
        <url>http://www.google.com</url>
    </event>
    // More Events...
</xml>

Now, it seems as though the XML is returned from the servlet as it should be, and the parsing of the data is working properly, too. However, the events themselves are not being rendered into the calendar. I'm not entirely sure what the difference is between events and eventSources, but I've tried both and had neither work properly. Any idea why -- for all intents and purposes -- the plug-in isn't rendering the events that seem to be within the events array as dictated by the documentation? Let me know if you need more details or code; I generated a mock of the necessities above, so it's not my whole code.

Upvotes: 0

Views: 2895

Answers (1)

Kris Schouw
Kris Schouw

Reputation: 352

I know that I haven't received any answers yet for this question, but I think I managed to figure out my issue. The <title> tags of the XML file my servlet was returning was sometimes invalid; some of the titles contained invalid XML characters that I needed to replace before shipping the file back to the AJAX call. Once that was done with the following function noted below, all of my events began rendering properly. And yes, I do realize that there is probably a more efficient way to remove invalid characters using regex or something, but everything I tried wasn't working. This function, though inefficient, works...

protected static String forXML(String aText) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character =  iterator.current();

    while (character != CharacterIterator.DONE ) {
        if (character == '<') {
            result.append("&lt;");
        }
        else if (character == '>') {
            result.append("&gt;");
        }
        else if (character == '\"') {
            result.append("&quot;");
        }
        else if (character == '\'') {
            result.append("&#039;");
        }
        else if (character == '&') {
            result.append("&amp;");
        }
        else {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

Upvotes: 1

Related Questions