Hi there,
I am using Symfony 7 which come with Turbo & Stimulus.
Every time I change page, the menu / dropdowns stop working.
I tried looking online for similar issue / solution, and the closest I got is few threads about Ruby using Turbo.
In most cases, they say the solution is to use KTComponents.init(); during the turbo:load event.
The only definition of KTComponents seems to be in .ts files (Typescript)?
But I just can't understand how to access KTComponents in my own javascript file as described in https://preview.keenthemes.com/html/metronic/docs/getting-started/customization/javascript
How to expose / access the KTComponents in a javascript file so that I can re-initialize them when turbo is loading a new page?
Thank you
PS: you don't have a symfony tag
Hi
Sorry for the delay in response. Here a user shared a guide on integrating Metronic 9 with Rails. We hope this information can be helpful.
https://devs.keenthemes.com/question/my-progress-on-using-metronic-9-with-rails-8-beta-1
Hello! I thought I had all my Medtronic 9 and Rails 8 / Turbo problems solved, but then, today, I realized that modals would not work after a turbo load.
So, I took a fresh look at things, and came up with a solution that, so far, fixes everything.
The problem is that some of the KT components need to be initialized after a turbo load, but there are a few variables, such as KT_DRAWER_INITIALIZED, that get set to "true" and as long as that variable is set to true, the component won't reinitialize.
So, the answer is to set them all to false, THEN reinitialize all KT components after turbo load.
So, I now have this as my application.js:
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
// Import Metronic 9 JS
import "core.bundle"
import "apexcharts.min"
// Reinitialize all Metronic compnents after Turbo has loaded
document.addEventListener("turbo:load", function() {
// After turbo has loaded, set several KT_***_INITIALIZED variables to false, and re-run the KTComponents.init() function.
// Some, or all, of these initializers will not re-run if the associated KT_***_INITIALIZED variable is set to true.
KT_DRAWER_INITIALIZED = false;
KT_DROPDOWN_INITIALIZED = false;
KT_MENU_INITIALIZED = false;
KT_MODAL_INITIALIZED = false;
KT_REPARENT_INITIALIZED = false;
KT_SCROLL_INITIALIZED = false;
KT_TABS_INITIALIZED = false;
KT_TOOLTIP_INITIALIZED = false;
// Reinitialize all KTComponents
KTComponents.init();
});
Hi
Sorry for the delay in response. Could you please try to access the component using window global variable? Eg.
window.KTComponents
Thanks
Hi,
Thank you for your reply.
I tried this approach as well, without success.
Here is the return when I try to access window.KTComponents in the console and my custom scripts
window.KTComponents
undefined
I need to access specific components like the table component and also KTComponents to re-init the menu or other sub components during the turbo lifecycle...
I tried many things, but can't seem to find a way to access any of these components in the compiled version of metronic (core.bundle.js)
If you want to replicate my issue,
I simply built the tailwind metronic version of the demo 1 and took all the html and transformed it into a twig file (symfony rendering engine) and replaced all the assets link to work with symfony. I have no error in the console and all files are loaded correctly (If I copy / paste a page or component from your demo site, it works perfectly fine)
For example, I need to make api calls to the backend to load data for the KTDatatable but I can't access the component and use custom options / configuration at the moment... I am stuck.
Thank you for your help