wagtail-deprecated / wagtail-react-streamfield

Powerful field for inserting multiple blocks with nesting. (NO LONGER MAINTAINED - See Wagtail 2.13 Release Notes)
https://wagtail.github.io/react-streamfield/public/
BSD 3-Clause "New" or "Revised" License
74 stars 23 forks source link

Recommended way to init JavaScript? #58

Closed globophobe closed 4 years ago

globophobe commented 4 years ago

Wagtail docs suggest js_initializer, but that didn't seem to work.

This worked:

def render_form(self, *args, **kwargs):
    form = super().render_form(*args, **kwargs)
    return form + "<script>jsInitializer()</script>"

What is the recommended way?

BertrandBordage commented 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?

globophobe commented 4 years ago

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.

BertrandBordage commented 4 years ago

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.