Last active
November 20, 2023 13:14
-
-
Save trouni/cb06fd7c6838e65c8f1e31f05f645c3a to your computer and use it in GitHub Desktop.
Revisions
-
trouni revised this gist
Nov 20, 2023 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -45,14 +45,16 @@ export default class extends Controller { } ``` 2. In your HTML view, use the `data-action` attribute to associate the action with an event. The syntax is `data-action="<event>-><name-of-your-controller>#<nameOfTheAction>"`: ```html <div data-controller="my-feature"> <button data-action="event->my-feature#doSomething">Click me</button> </div> ``` Here is the [list of all the JavaScript events](https://developer.mozilla.org/en-US/docs/Web/Events#event_listing) you can use. ## Select Elements using `data-target` 1. In your controller, use the `targets` property to define named references to HTML elements. You can then access those elements using `this.<targetName>Target`: @@ -73,12 +75,11 @@ export default class extends Controller { } ``` 2. In your HTML view, use the `data-target` attribute to indicate where the element should be referenced. The syntax is `data-<name-of-your-controller>-target="<target-name>`: ```html <div data-controller="my-feature"> <p data-my-feature-target="output"></p> </div> ``` -
trouni revised this gist
Aug 15, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -104,7 +104,7 @@ export default class extends Controller { 2. In your HTML view, use the `data-value` attribute to set the dynamic value on the same element your controller is connected to. The syntax is `data-<name-of-your-controller>-<name-of-the-value>-value="The value you want inside"`: ```html <div data-controller="my-feature" data-my-feature-greeting-value="Hello from data-value!"> <!-- Content here --> </div> ``` -
trouni revised this gist
Aug 15, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -104,7 +104,7 @@ export default class extends Controller { 2. In your HTML view, use the `data-value` attribute to set the dynamic value on the same element your controller is connected to. The syntax is `data-<name-of-your-controller>-<name-of-the-value>-value="The value you want inside"`: ```html <div data-controller="my-feature" data-my-feature-value="Hello from data-value!"> <!-- Content here --> </div> ``` -
trouni revised this gist
Aug 10, 2023 . 1 changed file with 10 additions and 15 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,14 +6,15 @@ - with `rails g stimulus my_feature`, or - by manually adding a file in the `controllers` directory of your Rails application, e.g., `app/javascript/controllers/my_feature_controller.js`. 2. The controller file should define your Stimulus controller class: ```javascript // app/javascript/controllers/my_feature_controller.js import { Controller } from "stimulus"; export default class extends Controller { connect() { // The connect action gets run as soon as an element connected to your controller is initialized console.log("MyFeatureController connected!"); } } @@ -33,12 +34,10 @@ export default class extends Controller { ```javascript // app/javascript/controllers/my_feature_controller.js // ... export default class extends Controller { // ... doSomething() { console.log("Button clicked!"); @@ -56,21 +55,19 @@ export default class extends Controller { ## Select Elements using `data-target` 1. In your controller, use the `targets` property to define named references to HTML elements. You can then access those elements using `this.<targetName>Target`: ```javascript // app/javascript/controllers/my_feature_controller.js // ... export default class extends Controller { static targets = ["output"]; // ... doSomething() { console.log(this.outputTarget); this.outputTarget.innerText = "Hello, Stimulus!"; } } @@ -91,14 +88,12 @@ export default class extends Controller { ```javascript // app/javascript/controllers/my_feature_controller.js // ... export default class extends Controller { static values = { greeting: String }; // ... initialize() { console.log("Initialized with greeting:", this.greetingValue); -
trouni revised this gist
Aug 10, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ - with `rails g stimulus my_feature`, or - by manually adding a file in the `controllers` directory of your Rails application, e.g., `app/javascript/controllers/my_feature_controller.js`. 2. The controller file should define your Stimulus controller class. The `connect` action gets run as soon as an element connected to your controller is initialized: ```javascript // app/javascript/controllers/my_feature_controller.js -
trouni revised this gist
Aug 10, 2023 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Cheat Sheet: Using Stimulus.js in Rails ## Create and Connect a Stimulus Controller 1. Create a new Stimulus controller file, either: - with `rails g stimulus my_feature`, or @@ -27,7 +27,7 @@ export default class extends Controller { </div> ``` ## Add Interactivity using `data-action` 1. In your controller, define a new method that will be triggered by an event (e.g. `doSomething`): @@ -54,7 +54,7 @@ export default class extends Controller { </div> ``` ## Select Elements using `data-target` 1. In your controller, use the `targets` property to define named references to HTML elements. You can then access those element using `this.<targetName>Target`: @@ -85,7 +85,7 @@ export default class extends Controller { </div> ``` ## Add Dynamic Values using `data-value` 1. In your controller, use the `values` property to define named references to dynamic values, and specify their datatype. They become accessible in your controller using `this.<valueName>Value`: -
trouni created this gist
Aug 10, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,115 @@ ### Cheat Sheet: Using Stimulus.js in Rails #### Create and Connect a Stimulus Controller 1. Create a new Stimulus controller file, either: - with `rails g stimulus my_feature`, or - by manually adding a file in the `controllers` directory of your Rails application, e.g., `app/javascript/controllers/my_feature_controller.js`. 2. The controller file should define your Stimulus controller class: ```javascript // app/javascript/controllers/my_feature_controller.js import { Controller } from "stimulus"; export default class extends Controller { connect() { console.log("MyFeatureController connected!"); } } ``` 3. In your HTML view, use the `data-controller` attribute to connect the controller to an element. The syntax is `data-controller=<name-of-your-controller>`: ```html <div data-controller="my-feature"> <!-- Your content here --> </div> ``` #### Add Interactivity using `data-action` 1. In your controller, define a new method that will be triggered by an event (e.g. `doSomething`): ```javascript // app/javascript/controllers/my_feature_controller.js import { Controller } from "stimulus"; export default class extends Controller { connect() { console.log("MyFeatureController connected!"); } doSomething() { console.log("Button clicked!"); } } ``` 2. In your HTML view, use the `data-action` attribute to associate the action with an event. The syntax is `data-action="<name-of-your-controller>#<nameOfTheAction>"`: ```html <div data-controller="my-feature"> <button data-action="my-feature#doSomething">Click me</button> </div> ``` #### Select Elements using `data-target` 1. In your controller, use the `targets` property to define named references to HTML elements. You can then access those element using `this.<targetName>Target`: ```javascript // app/javascript/controllers/my_feature_controller.js import { Controller } from "stimulus"; export default class extends Controller { static targets = ["output"]; connect() { console.log("MyFeatureController connected!"); } doSomething() { console.log("Button clicked!"); this.outputTarget.innerText = "Hello, Stimulus!"; } } ``` 2. In your HTML view, use the `data-target` attribute to indicate where the element should be referenced. The syntax is `data-target="<name-of-your-controller>.<target-name>`: ```html <div data-controller="my-feature"> <button data-action="my-feature#doSomething">Click me</button> <p data-target="my-feature.output"></p> </div> ``` #### Add Dynamic Values using `data-value` 1. In your controller, use the `values` property to define named references to dynamic values, and specify their datatype. They become accessible in your controller using `this.<valueName>Value`: ```javascript // app/javascript/controllers/my_feature_controller.js import { Controller } from "stimulus"; export default class extends Controller { static values = { greeting: String }; connect() { console.log("MyFeatureController connected!"); } initialize() { console.log("Initialized with greeting:", this.greetingValue); } } ``` 2. In your HTML view, use the `data-value` attribute to set the dynamic value on the same element your controller is connected to. The syntax is `data-<name-of-your-controller>-<name-of-the-value>-value="The value you want inside"`: ```html <div data-controller="my-feature" data-my-feature-greeting-value="Hello from data-value!"> <!-- Content here --> </div> ```