How it worksHow it works
JS Events are triggered every time change occurs. It can be triggered by an user interaction or reflects a programmatically change. Events are emitted in certain order as described in this documentation.
Events can be catched directly in your grid settings or in your own JS script.
There are three types of event listeners:
.on
: Allows you to listen to every occurrence of an event..once
: Allows you to listen to an event only once..off
: Allows you to detach or remove an event listener.
Events in grid settingsEvents in grid settings
All Event examples presented in this documentation can be directly applied in your grid settings under customization tab in the JS field and will be triggered only for this grid. In this case the variable wpgb
represents the main instance of the plugin holding all sub instances of the current grid.
console.dir( wpgb ); // Holds all instances.
// Where 'instanceName' can be: facets, grid, carousel, lightbox.
// Where 'eventName' is the event name attached to an instance.
wpgb.instanceName.on( 'eventName', function( args ) {
console.log( 'eventName', args );
} );
Events in external scriptEvents in external script
To use your own .js file to listen events from the plugin, you need at first to register your script to Gridbuilder ᵂᴾ thanks to this PHP filter:
function prefix_register_script( $scripts ) {
$scripts[] = [
'handle' => 'my-script',
'source' => 'my-url/script.js',
'version' => '1.0.0',
];
return $scripts;
}
add_filter( 'wp_grid_builder/frontend/register_scripts', 'prefix_register_script' );
Now that we have registered our own script we will be able to listen events from the plugin.
Because a page can contain several grids and facets (attached to each grid), we need to listen for each grid/template/content initialization thanks to the global event manager of the plugin stored in the global window object WP_Grid_Builder
:
// We listen every time a grid/template/content is initialized.
window.WP_Grid_Builder && WP_Grid_Builder.on( 'init', function( wpgb ) {
console.dir( wpgb ); // Holds all instances.
// Where 'instanceName' can be: facets, grid, carousel, lightbox.
// Where 'eventName' is the event name attached to an instance.
wpgb.instanceName.on( 'eventName', function( args ) {
console.log( 'eventName', args );
} );
} );