javascript

javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["checkbox", "selectAll", "count", "submit"]
  static values = { selectedIds: Array }

Stimulus: bulk selection + Turbo batch action

rails stimulus hotwire
by codesnips 4 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["input", "counter"]
  static values = {
    max: { type: Number, default: 280 }

Stimulus controller for dynamic form interactions

stimulus javascript hotwire
by Jordan Lee 2 tabs
erb
<div class="search-page">
  <div class="search-header mb-6">
    <h1 class="text-2xl font-bold mb-4">Search Posts</h1>

    <%= form_with url: posts_path,
        method: :get,

Live search with Turbo Frames and debouncing

hotwire turbo stimulus
by Jordan Lee 3 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
  belongs_to :author, class_name: "User"

  scope :visible, -> { where(deleted_at: nil).order(:created_at) }

Turbo Streams: partial replace with morphing (less jitter)

rails turbo hotwire
by codesnips 4 tabs
javascript
// 1. Basic WebSocket connection
const socket = new WebSocket('wss://example.com/ws');

// Connection opened
socket.addEventListener('open', (event) => {
  console.log('Connected to WebSocket server');

WebSockets for real-time bidirectional communication

websockets real-time javascript
by Alex Chang 1 tab
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["source"]

  async copy() {

Stimulus: copy-to-clipboard with fallback + selection

rails stimulus hotwire
by Sarah Chen 2 tabs
erb
<turbo-stream action="highlight" target="<%= dom_id(@project) %>">
  <template>
    <%= render @project %>
  </template>
</turbo-stream>

Custom turbo_stream action tag: highlight an updated element

rails hotwire turbo
by Henry Kim 3 tabs
javascript
class TokenBucket {
  constructor(capacity, refillPerSecond) {
    this.capacity = capacity;
    this.refillPerMs = refillPerSecond / 1000;
    this.tokens = capacity;
    this.lastRefill = Date.now();

Token Bucket Rate Limiter Middleware for Express with Per-Key Buckets

nodejs express rate-limiting
by codesnips 4 tabs
javascript
// ES6 Class syntax
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

JavaScript classes and prototype-based inheritance

javascript classes oop
by Alex Chang 1 tab
javascript
import { Controller } from "@hotwired/stimulus"
import { DirectUpload } from "@rails/activestorage"

export default class extends Controller {
  static targets = ["input", "preview", "status"]
  static values = {

Stimulus controller for drag-and-drop file uploads

stimulus javascript file-uploads
by Jordan Lee 2 tabs
javascript
// Sample data
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const users = [
  { id: 1, name: 'Alice', age: 25, active: true },
  { id: 2, name: 'Bob', age: 30, active: false },
  { id: 3, name: 'Charlie', age: 35, active: true },

Array methods: map, filter, reduce, and functional programming

javascript arrays functional-programming
by Alex Chang 1 tab
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["field"]

  connect() {

Stimulus: autofocus the first invalid field after Turbo update

rails stimulus hotwire
by codesnips 3 tabs