Project

General

Profile

Question about how to work with Stimulus in a plugin

Added by Tantic o 5 days ago

Hello,

I am working on a plugin https://github.com/tantic/redmine_asap_theme that uses Stimulus (dropdown menu, modal window, etc.). Up to version < 6.1, I imported Stimulus and my stimulus controllers, and it worked. Since version 6.1, Stimulus is directly imported into the Redmine core. So I removed my own import, and I would like to make sure that my stimulus controllers still work. Is there a specific procedure allowing plugins to use Stimulus?

I have tried several ways, but the most natural one for me was

 
/my_plugin 
 /assets 
  /javascripts 
   - application.js // with import of the controllers folder 
   /controllers 
    - my_controller.js 

In my_controller.js, I thought I could do something like that

 
import { Controller } from "@hotwired/stimulus"; 

export default class extends Controller { 
.... 
} 

But apparently, that's not enough.
Thank you in advance for your help.


Replies (1)

RE: Question about how to work with Stimulus in a plugin - Added by Tantic o 3 days ago

ok thanks to the patch by Vincent Robert it works for me too (thanks Vincent Robert)

Apparently we need to wait until Stimulus is fully loaded

https://github.com/nanego/redmine_multiprojects_issue/commit/62050fc6d3db54b46dbbf2df9afc5fc93d2b799e

(async function() {
  // Wait for Stimulus application to be available
  while (typeof Stimulus === 'undefined') {
    await new Promise(resolve => setTimeout(resolve, 100));
  }
  // Import Controller from Stimulus module
  const { Controller } = await import('@hotwired/stimulus');
  Stimulus.register("my-plugin-controller", class extends Controller {
...
    (1-1/1)