Created
August 22, 2016 08:39
-
-
Save timurvafin/17b00a29ab7907274ba6de974046d922 to your computer and use it in GitHub Desktop.
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
# app/assets/javascripts/toggleable_container.coffee | |
class @ToggleableContainer | |
constructor: (el) -> | |
@$el = $(el) | |
@$container = $("#" + @$el.data("toggleable")) | |
@_bindEvents() | |
_bindEvents: -> | |
@$el.on "change", @_toogle | |
_toogle: => | |
@$container.slideToggle() | |
new ToggleableContainer(el) for el in $("[data-toggleable]") |
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
<!-- spec/javascripts/fixtures/toggleable_container.html --> | |
<input type="checkbox" data-toggleable="hidden"> | |
<div id="hidden" style="display:none"> | |
I'm hidden | |
</div> | |
<input type="checkbox" data-toggleable="visible"> | |
<div id="visible"> | |
I'm visible | |
</div> |
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
# spec/javascripts/toggleable_container_spec.coffee | |
describe "ToggleableContainer", -> | |
beforeAll -> | |
loadFixtures("toggleable_container.html") | |
new ToggleableContainer(el) for el in $("[data-toggleable]") | |
it "displays hidden container on click", -> | |
$("[data-toggleable=hidden]").click() | |
expect($("#hidden")).toBeVisible() | |
it "hides container on click", -> | |
$("[data-toggleable=visible]").click() | |
expect($("#visible")).not.toBeVisible() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment