Skip to content
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

Dispatch magewire:available event when it actually is available #73

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/view/frontend/templates/page/js/magewire-initialize.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ $magewireScripts = $viewModels->require(Magewire::class);
window.livewire_app_url = '<?= $escaper->escapeUrl($magewireScripts->getApplicationUrl()) ?>'
window.livewire_token = window.hyva.getFormKey()

/* Make sure Magewire loads first. */
<?php /* Make sure Magewire loads first. */ ?>
if (window.Alpine) {
/* Defer showing the warning so it doesn't get buried under downstream errors. */
<?php /* Defer showing the warning so it doesn't get buried under downstream errors. */ ?>
document.addEventListener("DOMContentLoaded", () => {
setTimeout(function() {
console.warn("Magewire: It looks like AlpineJS has already been loaded. Make sure Magewire\'s scripts are loaded before AlpineJS.")
})
})
}

/* Initialize Alpine after Magewire */
<?php /* Initialize Alpine after Magewire */ ?>
(() => {
/* At this time Alpine is not yet loaded, so it is impossible to check how to defer Alpine initialization. */
<?php /* At this time Alpine is not yet loaded, so it is impossible to check how to defer Alpine initialization. */ ?>
const startMagewireOnce = () => {
document.removeEventListener('DOMContentLoaded', startMagewireOnce);
window.removeEventListener('alpine:init', startMagewireOnce);
window.magewire.start();
}

/* Initialize Alpine v2 after Magewire */
<?php /* Initialize Alpine v2 after Magewire */ ?>
const initAlpine = window.deferLoadingAlpine || ((callback) => callback());
window.deferLoadingAlpine = (callback) => {
window.addEventListener('magewire:load', () => {
Expand All @@ -64,24 +64,28 @@ $magewireScripts = $viewModels->require(Magewire::class);
}
document.addEventListener('DOMContentLoaded', startMagewireOnce)

/* Initialize Alpine v3 after Magewire */
<?php /* Init Alpine v3 after Magewire */ ?>
<?php /* (Note: with v3 alpine:init is dispatched before DOMContentLoaded. In v2 it is not used) */ ?>
window.addEventListener('alpine:init', startMagewireOnce);
})();

/* Dispatch "magewire:" prefixed window events. */
<?php /* Dispatch "magewire:" prefixed window events. */ ?>
Magewire.dispatchEvent = (name, event = {}) => {
document.dispatchEvent(new Event(`magewire:${ name }`, Object.assign({
'bubbles': true,
'cancelable': false
}, event)))
}

/* Mirror Livewire events. */
<?php /* Mirror Livewire events. */ ?>
['load', 'update'].forEach(function (target, index) {
document.addEventListener('livewire:' + target, (event) => {
Magewire.dispatchEvent(target, event)
})
})

<?php /* Allow component.initialize hooks to be registered before the components are initialized */ ?>
window.dispatchEvent(new Event('magewire:available'));
</script>

<?= /* @noEscape */ $block->getChildHtml('magewire.plugins') ?>
Expand Down
4 changes: 0 additions & 4 deletions src/view/frontend/templates/page/js/magewire-livewire.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ $magewireScripts = $viewModels->require(Magewire::class);
if (Alpine && Alpine.version.substring(0, 1) === '2' && ! name) return;
orig.call(this, name, value);
}
document.dispatchEvent(new Event('magewire:available', {
'bubbles': event.bubbles || true,
'cancelable': event.cancelable || true
}))
})
</script>

Expand Down