Skip to content

Commit

Permalink
Demo Turbo stream / Stimulus controller loading issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lewispb committed Feb 21, 2022
1 parent b1abf9e commit ce8aaa5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ gem "importmap-rails"
gem "turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
# gem "stimulus-rails"
gem "stimulus-rails", github: "hotwired/stimulus-rails"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
Expand Down
11 changes: 8 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: https://github.com/hotwired/stimulus-rails.git
revision: 993523e275c8fa5f8eecfb23fc607fdbde526533
specs:
stimulus-rails (1.0.3)
railties (>= 6.0.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -184,8 +191,6 @@ GEM
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
stimulus-rails (1.0.3)
railties (>= 6.0.0)
strscan (3.0.1)
thor (1.2.1)
timeout (0.2.0)
Expand Down Expand Up @@ -225,7 +230,7 @@ DEPENDENCIES
selenium-webdriver
sprockets-rails
sqlite3 (~> 1.4)
stimulus-rails
stimulus-rails!
turbo-rails
tzinfo-data
web-console
Expand Down
17 changes: 17 additions & 0 deletions app/javascript/controllers/auto_remove_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
connect() {
this.timeout = setTimeout(this.remove, 4000)
}

disconnect() {
clearTimeout(this.timeout)

this.remove()
}

remove = () => {
this.element.remove()
}
}
8 changes: 4 additions & 4 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import { application } from "controllers/application"

// Eager load all controllers defined in the import map under controllers/**/*_controller
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)
// import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
// eagerLoadControllersFrom("controllers", application)

// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
// import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
// lazyLoadControllersFrom("controllers", application)
import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
lazyLoadControllersFrom("controllers", application)
22 changes: 22 additions & 0 deletions app/javascript/controllers/turbo_simulator_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Controller } from "@hotwired/stimulus"

let elementIndex = 1

export default class extends Controller {
append() {
var htmlEl = document.getElementsByTagName("html")[0]

let template = `
<turbo-stream action="append" target="posts">
<template>
<div data-controller="auto-remove">
${elementIndex}. Hello world
</div>
</template>
</turbo-stream>
`

htmlEl.insertAdjacentHTML("beforeend", template)
elementIndex++;
}
}

0 comments on commit ce8aaa5

Please sign in to comment.