JS Events
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.
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.
[pastacode lang=”javascript” manual=”console.dir(%20wpgb%20)%3B%20%2F%2F%20Holds%20all%20instances.%0A%0A%0A%2F%2F%20Where%20’instanceName’%20can%20be%3A%20facets%2C%20grid%2C%20carousel%2C%20lightbox.%0A%2F%2F%20Where%20’eventName’%20is%20the%20event%20name%20attached%20to%20an%20instance%0A.wpgb.instanceName.on(%20’eventName’%2C%20function(%20args%20)%20%7B%20%0A%09%0A%09console.log(%20’eventName’%2C%20args%20)%3B%0A%0A%7D%20)%3B%0A” message=”” highlight=”” provider=”manual”/]
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:
[pastacode lang=”php” manual=”function%20prefix_register_script(%20%24scripts%20)%20%7B%0A%09%0A%09%24scripts%5B%5D%20%3D%20%5B%0A%09%09’handle’%20%3D%3E%20’my-script’%2C%0A%09%09’source’%20%3D%3E%20’my-url%2Fscript.js’%2C%0A%09%09’version’%20%3D%3E%20’1.0.0’%2C%20%0A%09%5D%3B%20%0A%09%0A%09return%20%24scripts%3B%0A%0A%7D%0A%0A%09%09%0Aadd_filter(%20’wp_grid_builder%2Ffrontend%2Fregister_scripts’%2C%20’prefix_register_script’%20)%3B” message=”” highlight=”” provider=”manual”/]
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 initialization thanks to the global event manager of the plugin stored in the global window object WP_Grid_Builder
[pastacode lang=”javascript” manual=”%2F%2F%20We%20listen%20every%20time%20a%20grid%2Ftemplate%20is%20initialized.%20%0Awindow.WP_Grid_Builder%20%26%26%20WP_Grid_Builder.on(%20’init’%2C%20function(%20wpgb%20)%20%7B%20%0A%09%0A%09console.dir(%20wpgb%20)%3B%20%2F%2F%20Holds%20all%20instances.%20%0A%09%0A%09%0A%09%2F%2F%20Where%20’instanceName’%20can%20be%3A%20facets%2C%20grid%2C%20carousel%2C%20lightbox.%20%0A%09%2F%2F%20Where%20’eventName’%20is%20the%20event%20name%20attached%20to%20an%20instance.%20%0A%09wpgb.instanceName.on(%20’eventName’%2C%20function(%20args%20)%20%7B%0A%09%09console.log(%20’eventName’%2C%20args%20)%3B%0A%09%7D%20)%3B%0A%0A%7D%20)%3B” message=”” highlight=”” provider=”manual”/]