Created
January 9, 2022 21:41
-
-
Save wrburgess/452b55c073ee16f57a1250b5b1a92954 to your computer and use it in GitHub Desktop.
Rails stimulus per-page or per-view controller actions. keywords: javascript, ruby, rails, stimulus, esbuild
This file contains 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 characters
<div | |
class="container-fluid admin <%= params[:controller] %> <%= params[:action] %>" | |
data-controller="<%= obj.model_name.plural.downcase %>" | |
data-<%= obj.model_name.plural.downcase %>-action="<%= params[:action] %>" | |
> | |
</div> |
This file contains 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 characters
<div class="container-fluid admin widgets new" data-controller="widgets" data-widgets-action="new"> | |
</div> |
This file contains 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 characters
import { Controller } from '@hotwired/stimulus' | |
const newConnect = function() { | |
console.log('action:', 'new', 'connected') | |
} | |
const editConnect = function() { | |
console.log('action:', 'new', 'connected') | |
} | |
const showConnect = function() { | |
console.log('action:', 'new', 'connected') | |
} | |
const indexConnect = function() { | |
console.log('action:', 'new', 'connected') | |
} | |
export default class extends Controller { | |
connect() { | |
const controllerAction = this.data.get('action') | |
switch (controllerAction) { | |
case 'edit': | |
editConnect() | |
break | |
case 'new': | |
newConnect() | |
break | |
case 'show': | |
showConnect() | |
break | |
case 'index': | |
indexConnect() | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment