// src/controllers/hello_controller.js
import { Controller } from "stimulus"
export default class extends Controller {
}
Identifiers link the DOM to the Controller
Action
Actions respond to DOM events
Targets
Targets Map Important Elements To Controller Properties
Let's try it together
All the stuff to enter can be found at:
http://bit.ly/sc2019-stimulus
Then open the controller at:
app/javascript/controllers/toggle_controller.js
Reload the page
You'll see in the log:
[Webpacker] Compiling...
and the message in the Javascript console
Action
app/views/works/index.html.erb:14
Files:
app/javascript/controllers/toggle_controller.js
toggle() {
console.log("click")
}
An action associates an arbitrary event with a controller method
event->controller#method
When the event is emitted, Stimulus invokes the controller method
The HTML element that triggers the event must be contained by the element the controller is bound to.
Target
A target is an HTML element of interest to the controller
...and then register with the controller
static targets = ["thingsToHide"]
static targets = ["thingsToHide"]
Targets make it easy to look up elements of interest
- this.thingsToHideTarget
- this.thingsToHideTargets
- this.hasThingToHideTarget
Let's update our action
toggle() {
this.thingsToHideTarget.classList.toggle("is-hidden")
}
Data Map
A Data Map allows you to associate data with a controller
Data maps belong to the controller
data-controllerName-key
- get(key)
- has(key)
- set(key, stringValue)
- delete(key)
We can tie it all together
Why Stimulus
Low overhead
Easy to follow
Remove boilerplate event binding
Predictable file structure
Easy to write small modular effects
Great at handling dynamic changes to the page
Why not Stimulus
No router / SPA
Not super-helpful if there's a lot of state
Webpack
"At its core, webpack is a static module bundler for modern JavaScript applications"
Entry
A file that references other files in use
Webpack uses entry points to build a dependency graph
Output
where the bundle ends up
often a single file
Loaders
transform files
For Example
Typescript compiler
CSS loader
File loader
So what is Webpacker?
"Webpacker makes it easy to use webpack to manage JavaScript in Rails"
Useful defaults
Makes some behavior easier to override
Webpacker is the default for new Rails 6 apps
./bin/rails webpacker:install
Important files:
package.json
/node_modules
config/webpacker.yml
config/webpack/
babel.config.js
postcss.config.js
Conventions:
Any file in application/javascript/packs is considered and entry point for a pack
Adding javascript_pack_tag or stylesheet_pack_tag adds the pack to the page
Pro tip: If you have both JS and CSS in the same pack, import them both in the same .js file, and use both javascript_pack_tag and stylesheet_pack_tag to load them
Rails automatically compiles when you hit a page in development
Or you can use
webpack-dev-server
which compiles when files change
and live reloads
Hands On
Let's build a different date filter
If no dates have been clicked, all dates show
If any dates have been clicked, only clicked dates show
What do we need?
- Controller
- Action for each date
- Action for show all
- Targets for each date body
- Data for current state