I want to fire an event for when AFTER a new widget has been added to the previewer, and then AFTER a widget has been removed.
Use Case: After the widget has been added to the previewer DOM, I want to change it's class based on how many other widgets are in that same sidebar area. So when
wp.customize('sidebars_widgets[sidebar-1]').get().length
fires, its a little too early to count all the widgets.
So like, I'm thinking I add a widget in the Customizer controls sidebar, then the previewer adds the widget, once the widget is added to the Previewer DOM I was hoping it would emit an event saying "this widget has been added". I could listen for that event and then run some code to change the class names on all the widgets in that sidebar.
I already have this working, by having the Customizer send some data to the Previewer, waiting 1 second, and then counting the widgets. But that doesnt seem very reliable (what if it takes 1.3 seconds for the widget to load?).
In my Previewer javascript (loaded via the customize_preview_init
hook, I have the following:
$( document ).on( 'widget-added', function(event, widget) {
console.log(widget);
});
But this doesn't seem to do anything.
Anyone know what that would be?