JS Methods
JS Methods are actions done by the plugin. Public methods allows to trigger an action programmatically and at any time.
Methods can be set directly in your grid settings or in your own JS script.
All Method examples presented in this documentation can be directly applied in your grid settings under customization tab in the JS field and will be done 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%2F%2F%20Where%20’instanceName’%20can%20be%3A%20facets%2C%20grid%2C%20carousel%2C%20lightbox.%0A%2F%2F%20Where%20’methodName’%20is%20the%20method%20name%20of%20the%20instance.%0Awpgb.instanceName.methodName()%3B” 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%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%0A%09%5D%3B%0A%0A%09return%20%24scripts%3B%0A%7D%0A%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 trigger methods 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%0A.window.WP_Grid_Builder%20%26%26%20WP_Grid_Builder.on(%20’init’%2C%20function(%20wpgb%20)%20%7B%20%0A%09%09%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.%0A%09%2F%2F%20Where%20’methodName’%20is%20the%20method%20name%20of%20the%20instance.%0A%09wpgb.instanceName.methodName()%3B%0A%0A%7D%20)%3B” message=”” highlight=”” provider=”manual”/]