Replies: 3 comments 13 replies
-
The thing is that with non-PJAX, object destruction and plugin destruction (usually plugin.destroy()) was not so important. Hovewer with SPA/PJAX it is important to clear plugins from memory before switchDOM. It should be done very close to actual switchDOM/swap to prevent short "blinks" caused by plugin.destroy(). |
Beta Was this translation helpful? Give feedback.
-
The const pjax = new Pjax({
selectors: ['element-that-contains-the-component'],
switches: {
'element-that-contains-the-component': (...args) => {
theComponentPlugin.destroy();
Pjax.switches.default(...args);
},
},
}); Does it suit your needs? |
Beta Was this translation helpful? Give feedback.
-
It seem to me that you're looking for a solution for something that is
caused by not constructing your app properly.
I've managed to achive a rather good pjax powered complex app using
laravel/webpack too.
Pm me if you'd like and i might be able to demonstrate.
בתאריך יום ב׳, 25 ביולי 2022, 11:58, מאת misog ***@***.***>:
… I won't add a such option for your "once" listeners because I think it
violates the specification, which can easily lead to strange problems if
some piece of your code or a lib you use (now or in a future version)
relies on the standard DOMContentLoaded events.
A way to differentiate PJAX from standard load would be useful in general.
DOM loaded by browser and DOM loaded by PJAX are results of different ways
of getting HTML.
If you really want it you can simply do it yourself:
document.addEventListener('pjax:success', () => {
document.dispatchEvent(new Event('DOMContentLoaded', { bubbles: true }));
});
Thanks, I will test this. However maybe I will still need to differentiate
PJAX from browser in some cases.
I still suggest you to use the switches
<https://github.com/PaperStrike/Pjax/tree/v2.4.0#switches> option, and to
use event-based algorithm, you can design your own events like:
I prefer handle my plugin instance locally in view Laravel view files;
init function which returns destroyhandler per each instance. I do not like
to handle lifecycle of plugins at one central place while creating Pjax
object.
So, instead of this:
<script>
var library;
// ...
document.addEventListener('destory-the-component', () => {
library.destroy();
});
</script>
I would like to use something like this:
<script>
var library;
// ...
library = new SomeLibrary();
document.addEventListener('pjax:just-right-before-dom-swap', () => {
library.destroy();
}, {once: true || "once just as the new keyword was executed once"});
</script>
—
Reply to this email directly, view it on GitHub
<#339 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAA3UVHZPCFI3VRJ5GNMJ5TVVZJMHANCNFSM54PN2OTA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi, currently there are events:
However I need to destroy some custom plugins which have DOM components just right before switchDOM is executed. But currently in Pjax I have to use
pjax:receive
event - but this event is many milliseconds away from switchDOM and the effect is that browser renders blank components after they were destroyed for a short time (before DOM is switched).I would need a hook or event that is executed just right before switchDOM and browser will not have time to render DOM changes up to the time of switchDOM.
Is it currently possible?
Thank you
Beta Was this translation helpful? Give feedback.
All reactions