-
Notifications
You must be signed in to change notification settings - Fork 427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Controllers don't reconnect after DOM mutations triggered by Morphdom #209
Comments
Are you mutating the DOM in such a way that the controller's element gets reconnected? Or, are you expecting another
|
My use case renders an HTML table (with a Stimulus controller) that has draggable rows. The drag/drop behavior is initialized in the table's I suspect that moving the row drag/drop event listener initialization to Stimulus actions may work. I'll try that and will report back shortly. |
Verified that initializing drag/drop with Stimulus actions works and is the correct way to handle this scenario. To be clear. I removed the drag/drop initialization from the table's connect method. // app/javascript/controllers/table_controller.js
import { Controller } from 'stimulus';
export default class extends Controller {
connect() {
let rows = this.element.querySelectorAll('tr[draggable]');
rows.forEach(tr => {
tr.addEventListener('dragstart', this.dragStart);
tr.addEventListener('dragend', this.dragEnd);
tr.addEventListener('dragover', this.dragOver);
tr.addEventListener('drop', this.drop);
});
}
// ...
} And I added Stimulus actions to each individual row. <table data-controller="table">
<tr draggable
data-action="dragstart->table#dragStart dragend->table#dragEnd dragover->table#dragOver drop->table#drop">
...
</tr>
</table> Thanks for being patient, letting me talk this out, and find the right solution. |
Nice! That's the Stimulus Way™ ;) BTW, your action descriptors are missing event names: <tr draggable data-action="dragstart->table#dragStart dragend->table#dragEnd …"> |
Oh whoops. I updated my example for posterity ;-) |
This may not be an issue with Stimulus but I wanted to bring it to your attention. I've noticed that Stimulus doesn't appear to recognize DOM mutations triggered by Morphdom.
I have a work around inspired by a conversation on a different issue.
For more context see: https://github.com/hopsoft/cable_ready#stimulus-gotchas
Note that adding the event listener for each connect is problematic. My actual project guards against adding redundant listeners.
The text was updated successfully, but these errors were encountered: