Hello,
First of all, thanks for this great framework.
For some reason when I try to dynamically attach elements coming from a remote source to the DOM, the data-action
attributes that each one of them defines doesn’t seem to work. Specifically, the redirect
function inside the controller never gets called.
<% @collection.compact.each do |resource| %>
<li class="result" data-action="click->autocomplete#redirect">
<%= resource.name %>
</li>
<% end %>
<div class="autocomplete" data-controller="autocomplete">
<!-- ... -->
<ul class="results hide" data-target="autocomplete.results"></ul>
<!-- ... -->
</div>
import { Controller } from "stimulus"
export default class extends Controller {
// ...
fetch() {
if (this.queryTarget.value.length > 0) {
fetch(`search/games?query=${this.queryTarget.value}`)
.then((response) => response.text())
.then(this.handleResults.bind(this))
} else {
this.handleEmptyQuery()
}
}
handleResults(html) {
this.resultsTarget.innerHTML = html
this.resultsTarget.classList.remove('hide')
}
handleEmptyQuery() {
this.resultsTarget.innerHTML = ''
this.resultsTarget.classList.add('hide')
}
redirect(arg) {
// NEVER GETS CALLED!!!
// NEVER GETS CALLED!!!
// NEVER GETS CALLED!!!
console.log('>>>', arg)
}
}
What could I be missing?
Thanks.