{"id":4916,"date":"2020-06-03T13:34:12","date_gmt":"2020-06-03T13:34:12","guid":{"rendered":"https:\/\/the7kennisbank.nl\/?p=4916"},"modified":"2020-06-09T13:08:35","modified_gmt":"2020-06-09T13:08:35","slug":"js-methods","status":"publish","type":"post","link":"https:\/\/the7kennisbank.nl\/en\/wp-grid-builder\/js-methods\/","title":{"rendered":"JS Methods"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element \" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<div>\n<div class=\"container flex items-center justify-between\">\n<h1 class=\"entry-title\">JS Methods<\/h1>\n<\/div>\n<\/div>\n<div id=\"article-wrapper\" class=\"container\">\n<p>JS Methods are actions done by the plugin. Public methods allows to trigger an action programmatically and at any time.<\/p>\n<p>Methods can be set directly in your grid settings or in your own JS script.<\/p>\n<p>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 <code>wpgb<\/code> represents the main instance of the plugin holding all sub instances of the current grid.<\/p>\n<div class=\"code-syntax-block code-syntax-wrapper\">\n<p>[pastacode lang=&#8221;javascript&#8221; manual=&#8221;console.dir(%20wpgb%20)%3B%20%2F%2F%20Holds%20all%20instances.%0A%0A%2F%2F%20Where%20&#8217;instanceName&#8217;%20can%20be%3A%20facets%2C%20grid%2C%20carousel%2C%20lightbox.%0A%2F%2F%20Where%20&#8217;methodName&#8217;%20is%20the%20method%20name%20of%20the%20instance.%0Awpgb.instanceName.methodName()%3B&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n<\/div>\n<p>To use your own .js file to listen events from the plugin, you need at first to register your script to Gridbuilder \u1d42\u1d3e thanks to this PHP filter:<\/p>\n<div class=\"code-syntax-block code-syntax-wrapper\">\n<p>[pastacode lang=&#8221;php&#8221; manual=&#8221;function%20prefix_register_script(%20%24scripts%20)%20%7B%0A%0A%09%24scripts%5B%5D%20%3D%20%5B%0A%09%09&#8217;handle&#8217;%20%3D%3E%20&#8217;my-script&#8217;%2C%0A%09%09&#8217;source&#8217;%20%3D%3E%20&#8217;my-url%2Fscript.js&#8217;%2C%0A%09%09&#8217;version&#8217;%20%3D%3E%20&#8217;1.0.0&#8217;%2C%0A%09%5D%3B%0A%0A%09return%20%24scripts%3B%0A%7D%0A%0Aadd_filter(%20&#8217;wp_grid_builder%2Ffrontend%2Fregister_scripts&#8217;%2C%20&#8217;prefix_register_script&#8217;%20)%3B&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n<\/div>\n<p>Now that we have registered our own script we will be able to trigger methods from the plugin.<\/p>\n<p>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 <code>WP_Grid_Builder<\/code><\/p>\n<div class=\"code-syntax-block code-syntax-wrapper\">\n<p>[pastacode lang=&#8221;javascript&#8221; manual=&#8221;%2F%2F%20We%20listen%20every%20time%20a%20grid%2Ftemplate%20is%20initialized%0A.window.WP_Grid_Builder%20%26%26%20WP_Grid_Builder.on(%20&#8217;init&#8217;%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&#8217;instanceName&#8217;%20can%20be%3A%20facets%2C%20grid%2C%20carousel%2C%20lightbox.%0A%09%2F%2F%20Where%20&#8217;methodName&#8217;%20is%20the%20method%20name%20of%20the%20instance.%0A%09wpgb.instanceName.methodName()%3B%0A%0A%7D%20)%3B&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n<\/div>\n<\/div>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":4996,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[44,54,43],"tags":[],"class_list":["post-4916","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-documentation","category-js-methods","category-wp-grid-builder","category-44","category-54","category-43","description-off"],"acf":[],"_links":{"self":[{"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/posts\/4916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/comments?post=4916"}],"version-history":[{"count":2,"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/posts\/4916\/revisions"}],"predecessor-version":[{"id":5094,"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/posts\/4916\/revisions\/5094"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/media\/4996"}],"wp:attachment":[{"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/media?parent=4916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/categories?post=4916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the7kennisbank.nl\/en\/wp-json\/wp\/v2\/tags?post=4916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}