Created
September 12, 2012 22:03
-
-
Save rmurphey/3710261 to your computer and use it in GitHub Desktop.
DRY Out Your Code with Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// repetitive code! | |
function updateDates() { | |
var startDate = $("#start-date").datepicker("getDate"); | |
$("#menu-item-1 .day").text(startDate.getDate()); | |
$("#menu-item-1 .month").text(Util.monthToText(startDate.getMonth())); | |
$("#menu-item-1 .year").text(startDate.getFullYear()); | |
var endDate = $("#end-date").datepicker("getDate"); | |
$("#menu-item-2 .day").text(endDate.getDate()); | |
$("#menu-item-2 .month").text(Util.monthToText(endDate.getMonth())); | |
$("#menu-item-2 .year").text(endDate.getFullYear()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateDates() { | |
updateRegions('#start-date', '#menu-item-1'); | |
updateRegions('#end-date', '#menu-item-2'); | |
function updateRegions(datepickerElement, targetElement) { | |
var date = $(datepickerElement).datepicker('getDate'); | |
var target = $(targetElement); | |
target.find(".day").text(date.getDate()); | |
target.find(".month").text(Util.monthToText(date.getMonth())); | |
target.find(".year").text(date.getFullYear()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a little thing...
In the refactored code above, the updateRegions function will be created every time updateDates is called, won't it?