Skip to content

Instantly share code, notes, and snippets.

@RolandStuder
Last active January 12, 2021 15:07
Show Gist options
  • Save RolandStuder/7cc6851aaf83c42e8090415cbf7344f0 to your computer and use it in GitHub Desktop.
Save RolandStuder/7cc6851aaf83c42e8090415cbf7344f0 to your computer and use it in GitHub Desktop.
stimulus-reflex-lazy-loader
import ApplicationController from './application_controller'
import StimulusReflex from "stimulus_reflex";
export default class extends ApplicationController {
connect(){
StimulusReflex.register(this)
if (!this.element.id) {
this.element.id = this.randomId()
}
this.loadReflex()
}
loadReflex(){
if (this.isActionCableConnectionOpen()) {
this.stimulate(this.data.get('reflex'), this.element)
} else {
setTimeout(function () {
this.loadReflex()
}.bind(this), 10);
}
}
randomId() {
return Math.random().toString(32).replace(/[^a-z]+/g, '').substr(2, 10);
}
}
<div class="div" data-controller="lazy-load" data-lazy-load-reflex="ExampleReflex#test">
Placeholder
</div>
@RolandStuder
Copy link
Author

It's been a while and I don't actually use this, but it used to work…

I don't have time right now to look into it. But I would recommend to use https://github.com/julianrubisch/futurism/ this is actually being worked on and will be more solid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment