OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

$emitter.subscribe for event inside init() not working

  • Thread starter Thread starter Carsten
  • Start date Start date
C

Carsten

Guest
I have a JS plugin in Shopware 6, that subscribes to two events of another JS plugin:

One that fires after the user interacts with the page and one that fires at the end of init().

The latter does nothing. I am confused, because the other event works just fine.

Both $emitter.publish have the same argument. In my plugin, they use the same method:

Code:
// main.js
import MyPlugin from "./my-plugin/my-plugin.plugin";

const PluginManager = window.PluginManager;
PluginManager.register("MyPlugin", MyPlugin);

Code:
// ./my-plugin/my-plugin.plugin.js
import Iterator from "src/helper/iterator.helper";

export default class MyPlugin extends window.PluginBaseClass {
  init() {
    const configuratorPlugins =
      window.PluginManager.getPluginInstances("TargetPlugin");
    Iterator.iterate(configuratorPlugins, (instance) => {
      // nope ...
      instance.$emitter.subscribe("init", this.onEvent.bind(this));
      // working ...
      instance.$emitter.subscribe(
        "otherEvent",
        this.onEvent.bind(this)
      );
    });
  }

  onEvent(event) {
    // do it ...
  }
}

Code:
// target-plugin.plugin.js
    init() {
        // ...
        this.$emitter.publish('init', this.createCurrentState());
    }

The init event is not inside an if statement. I tested this.createCurrentState() with a console.log() right before the event and that works.

Also, I looked into PluginManager.getPluginList(). The other plugin is in the list before my plugin gets registered – if that helps.

Don't know what I am doing wrong. That plugin is the only one which publishes an event inside init().

<p>I have a JS plugin in Shopware 6, that subscribes to two events of another JS plugin:</p>
<p>One that fires after the user interacts with the page and one that fires at the end of <code>init()</code>.</p>
<p>The latter does nothing. I am confused, because the other event works just fine.</p>
<p>Both <code>$emitter.publish</code> have the same argument. In my plugin, they use the same method:</p>
<pre class="lang-js prettyprint-override"><code>// main.js
import MyPlugin from "./my-plugin/my-plugin.plugin";

const PluginManager = window.PluginManager;
PluginManager.register("MyPlugin", MyPlugin);
</code></pre>
<pre class="lang-js prettyprint-override"><code>// ./my-plugin/my-plugin.plugin.js
import Iterator from "src/helper/iterator.helper";

export default class MyPlugin extends window.PluginBaseClass {
init() {
const configuratorPlugins =
window.PluginManager.getPluginInstances("TargetPlugin");
Iterator.iterate(configuratorPlugins, (instance) => {
// nope ...
instance.$emitter.subscribe("init", this.onEvent.bind(this));
// working ...
instance.$emitter.subscribe(
"otherEvent",
this.onEvent.bind(this)
);
});
}

onEvent(event) {
// do it ...
}
}
</code></pre>
<pre class="lang-js prettyprint-override"><code>// target-plugin.plugin.js
init() {
// ...
this.$emitter.publish('init', this.createCurrentState());
}
</code></pre>
<p>The <code>init</code> event is not inside an if statement. I tested <code>this.createCurrentState()</code> with a <code>console.log()</code> right before the event and that works.</p>
<p>Also, I looked into <code>PluginManager.getPluginList()</code>. The other plugin is in the list before my plugin gets registered – if that helps.</p>
<p>Don't know what I am doing wrong. That plugin is the only one which publishes an event inside <code>init()</code>.</p>
 

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top