Closed globophobe closed 4 years ago
That is right, the Python method js_initializer
is removed with React StreamField.
Could you point to me the part of the Wagtail documentation mentioning js_initializer
? I cannot find it.
js_initializer
is just used to initialize the StreamField structure itself, not its block widgets.
So I’m also wondering: what is your use case? What are you trying to initialize, the JS from a block widget, or the JS from the StreamField itself?
Oops, not the docs. I think it was https://github.com/wagtail/wagtail/wiki/StreamField-blocks-API#js_initializerself
I would like to init JS of a block widget.
It works like the existing StreamField. Include an inline <script>
tag at the end of your widget’s template.
Look at Wagtail for examples. For the date widget, it defines a custom widget that renders this template containing a script. It also references this JavaScript file that contains the front-end logic. It’s better to put as much logic in a JS file like this, so that each new block just contains a script like <script>initWidget("{{ widget.attrs.id|escapejs }}")</script>
, reducing the browser loading time.
Wagtail docs suggest
js_initializer
, but that didn't seem to work.This worked:
What is the recommended way?