Skip to content

Instantly share code, notes, and snippets.

@rgadon107
Last active January 31, 2021 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgadon107/88532d2df2a8207268fb1edddb1ec111 to your computer and use it in GitHub Desktop.
Save rgadon107/88532d2df2a8207268fb1edddb1ec111 to your computer and use it in GitHub Desktop.
Solution to Code Challenge 7 from "Refactoring Tweaks Workbook"
/* 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