Skip to content

Instantly share code, notes, and snippets.

@ayb
ayb / stimulus.md
Last active October 22, 2020 15:54 — forked from mrmartineau/stimulus.md
Stimulus cheatsheet

Stimulus cheatsheet

Lifecycle callbacks

  • initialize: once, when the controller is first instantiated
  • connect: anytime the controller is connected to the DOM
  • disconnect: anytime the controller is disconnected from the DOM

Action descriptors

The data-action value click->hello#greet is called an action descriptor. This particular descriptor says:

class ArrayCalculator
TEST_ONE = [-7, 1, 5, 2, -4, 3, 0]
TEST_TWO = [-7, 1, 5, 2, -4, 3]
#= calculate(:at => 3, :arr => [-7, 1, 5, 2, -4, 3, 0])
def self.balanced?(args = {})
raise ArgumentError unless args.has_key?(:at) and args.has_key?(:arr)
left_sum, right_sum = 0, 0
(0..(args[:at]-1)).each { |n| left_sum += args[:arr][n] }
require 'rubygems'
require 'sinatra'
require 'active_support'
get "/message" do
response = { :Say => "Hello, I am a big monkey" }
response.to_xml(:root => 'Response')
end
post "/message" do