Last active
January 31, 2021 00:10
-
-
Save rgadon107/88532d2df2a8207268fb1edddb1ec111 to your computer and use it in GitHub Desktop.
Solution to Code Challenge 7 from "Refactoring Tweaks Workbook"
This file contains hidden or 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
/* Code Challenge 7, from “Refactoring Tweaks Workbook” by Tonya Mork ( 2016, LeanPub (https://leanpub.com/littlegreenbookofrefactoringtweaks-workbook) ). */ | |
/*************************************************** | |
* | |
* Original code to be refactored. | |
* | |
**************************************************/ | |
window.PluginName = (function(window, document, $, undefined){ | |
'use strict'; | |
var plugin = { | |
.. | |
} | |
.. | |
plugin.toggleCheckBoxes = function( event ) { | |
event.preventDefault(); | |
var $this = $( this ); | |
var $multicheck = $this.closest( '.plugin-td' ).find( 'input[type=checkbox]:not([disabled])' ); | |
// If the button has already been clicked once... | |
if ( $this.data( 'checked' ) ) { | |
// clear the checkboxes and remove the flag | |
$multicheck.prop( 'checked', false ); | |
$this.data( 'checked', false ); | |
} | |
// Otherwise mark the checkboxes and add a flag | |
else { | |
$multicheck.prop( 'checked', true ); | |
$this.data( 'checked', true ); | |
} | |
}; | |
.. | |
return plugin; | |
})(window, document, jQuery); | |
/*************************************************** | |
* | |
* Refactored Code | |
* | |
**************************************************/ | |
window.PluginName = (function(window, document, $, undefined){ | |
'use strict'; | |
var plugin = { | |
.. | |
} | |
.. | |
plugin.toggleCheckBoxes = function( event ) { | |
event.preventDefault(); | |
var $this = $( this ); | |
var $multicheck = $this.closest( '.plugin-td' ).find( 'input[type=checkbox]:not([disabled])' ); | |
if ( plugin.multiCheckIsChecked() ) { | |
$multicheck.prop( 'checked', false ); | |
$this.data( 'checked', false ); | |
} | |
if ( ! plugin.multiCheckIsChecked() ) { | |
$multicheck.prop( 'checked', true ); | |
$this.data( 'checked', true ); | |
} | |
}; | |
plugin.multiCheckIsChecked = function() { | |
if ( $multicheck.is( $this.data( 'checked' ) ) ); | |
return true; | |
}; | |
.. | |
return plugin; | |
})(window, document, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment