Skip to content

[Hard question] avoid re-run of Stimulus on DOM modifications? #522

@gremo

Description

@gremo

I'll try to explain what I'm asking as simple as possible.

I'm writing a controller named parent. This is the starting HTML:

<div data-controller="parent">
    <!-- ... -->
</div>

After the controller kicks in, the DOM will be modified this way (because how the JavaScript library I'm using):

<div class="wrapper">
    <div data-controller="parent">
        <!-- ... -->
    </div>
</div>

Problem 1: the controller named parent re-run. This is not my intended behavior. I've managed to fix it using the plugin onInit hook, simply deleting the data-controller attribute:

connect() {
  new MyPlugin(this.element, {
    onInit: () => {
      delete this.element.dataset.controller; // <-- this will avoid re-run of parent controller
    }
  });
}

Problem 2 the problem is much worst with "child" controllers. Stimulus will run twice: on load and on DOM modifications:

<div class="wrapper">
    <div data-controller="parent">
        <div data-controller="child"></div>
    </div>
</div>

Any idea on how to solve this (like "disable watching DOM modifications" is much appreciated, thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions