admin管理员组文章数量:1129201
I'm trying to make a Custom Gutenberg Block, but I'm having trouble with loading javascript that goes with this block.
On the front-end it works fine, but in the editor it seems that the script fires (even with DOMcontentloaded or window.onload) BEFORE the Gutenberg block itself is fully loaded.
Since my script targets an element in the block (which it can't find because the element hasn't loaded yet), it's not working correctly.
Any suggestions on how to make the script load AFTER the Custom Block has loaded fully in the editor?
(I'm using the ACF way of creating Custom blocks)
I'm trying to make a Custom Gutenberg Block, but I'm having trouble with loading javascript that goes with this block.
On the front-end it works fine, but in the editor it seems that the script fires (even with DOMcontentloaded or window.onload) BEFORE the Gutenberg block itself is fully loaded.
Since my script targets an element in the block (which it can't find because the element hasn't loaded yet), it's not working correctly.
Any suggestions on how to make the script load AFTER the Custom Block has loaded fully in the editor?
(I'm using the ACF way of creating Custom blocks)
Share Improve this question asked Sep 8, 2019 at 11:54 J. StocklandJ. Stockland 1156 bronze badges1 Answer
Reset to default 1I couldn't find a built in ACF way of doing this.
Instead, in my block's PHP render function I added
printf( "<script>window.jQuery(window).trigger('acf/loaded/block-name');</script>" );
This uses jQuery as an event bus to trigger an event when the block is rendered. You might need to include a check that you're in the admin so the event won't be triggered in the frontend. My WordPress is headless so this wasn't an issue for me.
Then, in your JavaScript, you can do:
$( window ).on( 'acf/loaded/block-name', function() {
// rest of your code here
} )
By including the block name in the event you can check different events for individual blocks.
本文标签: customizationHow to make a script load after Custom Block is loaded in the editor
版权声明:本文标题:customization - How to make a script load after Custom Block is loaded in the editor? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736738027a1950354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论