Rails 6, Stimulus events
Associate Software Engineer (Ruby on Rails) at Main Street

Rails 6, Stimulus events

An action is a connection between:

  • a controller method
  • the controller’s element
  • a DOM event listener

gallery_controller.js

// controllers/gallery_controller.js
import { Controller } from "stimulus"

export default class extends Controller {
  next(event) {
    alert("Justclick !!! :")
  }
}

View File Setup

<div data-controller="gallery">
  <button data-action="click->gallery#next">…</button>
</div>

Descriptors

The data-action value click->gallery#next is called an action descriptor. In this descriptor:

  • click is the name of the DOM event to listen for
  • the gallery is the controller identifier
  • next is the name of the method to invoke

The full set of these shorthand pairs is as follows:

I hope that helps someone. Thanks :).

Thanks for reading. ??

I’d love to hear thoughts or comments around this. Feel free to email me at [email protected] or hit me up on twitter, @ronakabhattrz .

Alex Shute

Focused on the 80/20 of SEO

2 年

Ronak, thanks for sharing!

要查看或添加评论,请登录

Ronak Bhatt的更多文章

  • Add multiple Subdomains in Ruby On Rails 7

    Add multiple Subdomains in Ruby On Rails 7

    Subdomains are a great way to organize different parts of your app. You might have an subdomain, subdomain, etc.

    1 条评论
  • Rails 6, Stimulus and Select2

    Rails 6, Stimulus and Select2

    This is just a quicky, to help out anyone using Rails 6, or any Stimulus-enabled Rails project and Select2, the amazing…

    11 条评论

社区洞察

其他会员也浏览了