02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

282<br />

CHAPTER 8 ■ EDITING THE CALENDAR WITH AJAX AND JQUERY<br />

};<br />

},<br />

// GMT, then adjusted for the local timezone,<br />

// adjust the offset to ensure a proper date<br />

event.setMinutes(event.getTimezoneOffset());<br />

"deserialize" : function(str){...},<br />

"urldecode" : function(str) {...}<br />

Ensuring the Event Occurs in the Current Month<br />

Your next step is to set up a conditional statement that ensures that only events that belong on the<br />

calendar are appended. If both the year <strong>and</strong> month match between the current calendar month <strong>and</strong> the<br />

event date, you can extract the day of the month using the Date object’s .getDay() method. To work<br />

properly with the next step, which adds leading zeroes to single-digit dates, you also need to convert this<br />

value to a string, which is accomplished <strong>by</strong> passing the value to String().<br />

The day of the month needs to have a leading zero to properly match the calendar. For example, if<br />

the returned date is only one digit, you prepend a leading zero to the date.<br />

Do this <strong>by</strong> inserting the following bold code:<br />

fx = {<br />

"initModal" : function() {...},<br />

"boxin" : function(data, modal) {...},<br />

"boxout" : function(event) {...},<br />

// Adds a new event to the markup after saving<br />

"addevent" : function(data, formData){<br />

// Converts the query string to an object<br />

var entry = fx.deserialize(formData),<br />

// Makes a date object for current month<br />

cal = new Date(NaN),<br />

// Makes a date object for the new event<br />

event = new Date(NaN),<br />

// Extracts the event day, month, <strong>and</strong> year<br />

date = entry.event_start.split(' ')[0],<br />

// Splits the event data into pieces<br />

edata = date.split('-'),<br />

// Extracts the calendar month from the H2 ID<br />

cdata = $("h2").attr("id").split('-');<br />

// Sets the date for the calendar date object<br />

cal.setFullYear(cdata[1], cdata[2], 1);<br />

// Sets the date for the event date object<br />

event.setFullYear(edata[0], edata[1], edata[2]);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!