You can get the list of widgets in a sidebar via:
wp.customize('sidebars_widgets[sidebar-1]').get()
This is a list of the widgets’ IDs. The sidebars_widgets[sidebar-1]
is the setting ID for the sidebar. Replace sidebar-1
with the ID of your sidebar.
So to get the count just do:
wp.customize('sidebars_widgets[sidebar-1]').get().length
If you want to listen for when a widget is added or removed to a sidebar, you can bind
to the setting
to listen for changes, like this:
wp.customize( 'sidebars_widgets[sidebar-1]', function( sidebarSetting ) {
sidebarSetting.bind( function( newWidgetIds, oldWidgetIds ) {
console.info( {
added: _.difference( newWidgetIds, oldWidgetIds ),
removed: _.difference( oldWidgetIds, newWidgetIds )
} );
} )
} );