Forked from noelrappin/01_declaring_controller.html
Last active
October 22, 2019 19:26
-
-
Save jcoyne/b58fa2c327bc076a0eef184ca6ddb512 to your computer and use it in GitHub Desktop.
Copy paste parts from Stimulus Workshop
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
# app/views/schedule/index.html.erb:29-30 | |
<section class="day-body" | |
id="day-body-<%= schedule_day.day.by_example("2006-01-02") %>" | |
data-controller="toggle"> |
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
// app/javascript/controllers/toggle_controller.js | |
import { Controller } from "stimulus" | |
export default class extends Controller { | |
connect() { | |
console.log("The Controller is Connected") | |
} | |
} |
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
# app/views/works/index.html.erb:14 | |
Files: <button data-action="click->toggle#toggle">Hide</button> |
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
# in the class in app/javascript/controllers/toggle_controller.rb | |
toggle() { | |
console.log("click") | |
} |
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
# app/views/works/index.html.erb:39 | |
<section data-target="toggle.thingToHide"> |
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
# in the class in app/javascript/controllers/toggle_controller.rb | |
static targets = ["thingToHide"] |
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
# in the class in app/javascript/controllers/toggle_controller.rb | |
toggle() { | |
this.thingToHideTarget.classList.toggle("is-hidden") | |
} |
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
# in app/views/works/index.html.haml | |
<section class="day-body" | |
id="day-body-<%= schedule_day.day.by_example("2006-01-02") %>" | |
data-controller="toggle" | |
data-toggle-hidden="false"> |
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
<span data-target="toggle.text">Hide</span> |
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
import { Controller } from "stimulus" | |
export default class extends Controller { | |
static targets = ["text", "thingToHide"] | |
isHidden() { return this.data.get("hidden") === "true" } | |
flip() { this.data.set("hidden", this.isHidden() ? "false" : "true") } | |
toggle() { | |
this.flip() | |
this.thingToHideTarget.classList.toggle("is-hidden", this.isHidden()) | |
this.textTarget.innerText = this.isHidden() ? "Show" : "Hide" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment