Last active
August 29, 2015 14:05
-
-
Save zdavis/b713e3eb96e94e90834b to your computer and use it in GitHub Desktop.
A simple CCS-based switcher in JS
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
<input type="checkbox" value="1" /> | |
class Switcher | |
construct: ($el, options) -> | |
@$el = $el | |
@$switcherEl = 'some template containin markup for the switcher' | |
@$el.append(@$switcherEl) | |
@state = @$el.prop('checked') | |
@initEvents() | |
initEvents: () -> | |
@$el.click(() => @toggle) | |
changeState: (to) -> | |
if to == true | |
@$switcherEl.addClass('checked') | |
@$el.attr('checked', true) | |
else | |
@$switcherEl.removeClass('checked') | |
@$el.attr('checked', false) | |
toggle: () -> | |
if @state == true | |
@changeState(false) | |
else | |
@changeState(true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment