forwarder / belco-magento2

2 stars 9 forks source link

Fire Event when Belco is ready #2

Closed wouter-toppy closed 4 years ago

wouter-toppy commented 4 years ago

See issue #1

You will be able to listen to the event with:

document.addEventListener(
    'Belco_Ready',
    function (e) {
        // $('#belco-container') is available
    },
    false
);
Pagebakers commented 4 years ago

Our initialization code is loaded async by Magento2, you can use the Magento2 commonjs library to achieve the same;

I haven't tested this, but i believe it should work.

define([
  'Belco_Widget/js/view/belco-config'
], function () {
  Belco.on('ready, function() {
      // $('#belco-container') is available
  })
})
wouter-toppy commented 4 years ago

@Pagebakers The thing with your implentation is, my custom module (like the cookie popup module) is fully depending on the belco module. which is not nessecary and totally not needed. when we remove the belco module. or it breaks somehow the cookie module also breaks. because your file isnt there. or it isnt ready.

when you fire an global JS event with a unique name. i can listen to that event based on the name and do some actions. in that case. my module doesnt depent on the belco module. it simply listens to an event by its name. if that event isnt fired. its fine and continues ;-)

Pagebakers commented 4 years ago

Does the entire define clause fail? or does it load and is window.Belco undefined?

otherwise you could check if the variable exists

if (window.Belco !== undefined) {
Belco...
}
wouter-toppy commented 4 years ago

@Pagebakers thats just the point. even if the define fails or succeed. there is no need for the cookie module to load it in the define and check if that variable is or isnt defined. its an ugly dependency you dont want to have. the cookie module doesnt need to know if its there. and if its defined, or ready. it only needs to do something when an eventname is triggered.

in the other case. when i'm having to check if belco isnt defined or not outside a define/require statement there is no way to do that. and when the scripts load in priority. when my cookie script is running before the belco scipt. it checks if its defined. if it isnt because the belco script isnt initialized it wont run the code. and never gets back to that part of code when finaly belco is loaded.

with events you can set a piece of code which needs to run when belco is loaded

Pagebakers commented 4 years ago

I get what you are saying, but from our point of view we rather don't add extra global events because this changes our APIs only for this use case in Magento2, it adds extra complexity that we have to take into account.

I just looked into requirejs and it seems it doesnt support optional dependencies, so that doesn't seem to be a solution either.

I'll have to think about this a bit more to see how we can best implement this.

wouter-toppy commented 4 years ago

@Pagebakers any update. meaby we can call today? you have my phonenumber ;-)

Pagebakers commented 4 years ago

@wouter-toppy

I didn't come up with a better solution, so merged this and made a small change. Thanks a bunch :)

The event name is now 'BelcoReady'

Version 0.4.1 is now available.

Are there other things you want to call about? please send me a message on our chat if so!