Last active
December 19, 2015 15:29
-
-
Save svperfecta/5976678 to your computer and use it in GitHub Desktop.
Javascript Class Pattern
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
var Toggler = (function() { | |
var totalTogglers = 0; | |
var Toggler = function() { | |
this.linkSelector = ".detail_toggle"; | |
this.detailSelector = ".detail"; | |
this.hiddenClass = "hidden"; | |
this.hideText = "Hide Details"; | |
this.showText = "Show Details"; | |
}; | |
Toggler.prototype = { | |
init: function() { | |
var toggler = this; | |
$(this.linkSelector).on("click", function(event) { | |
toggler.toggleOnClick(event) | |
}); | |
}, | |
toggleOnClick: function(event) { | |
this.link = $(event.target); | |
this.link.text(this.isDetailHidden() ? this.hideText : this.showText); | |
this.detailElement().toggleClass(this.hiddenClass); | |
event.preventDefault(); | |
}, | |
detailElement: function() { | |
return this.link.parent().find(this.detailSelector) | |
}, | |
isDetailHidden: function() { | |
return this.detailElement().hasClass(this.hiddenClass); | |
} | |
}; | |
return Toggler; | |
})(); | |
$(function() { | |
var toggler = new Toggler(); | |
toggler.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment