I originally asked this question on the wagtail issue queue, which I guess was the wrong place for it. (Though I do think this is a bug in the documentation.)
Anyway, my issue is that I have a custom StructBlock
class that uses a ListBlock
inside it. I need to define a js_initializer()
method on my custom class that causes the form to trigger both my own initializer and ListBlock
's initializer.
My initial attempt, based on the docs, looked like this:
# my_blocks.py
class ImageGalleryBlock(blocks.StructBlock):
images = ListBlock(ImageChooserBlock(label='Image'))
def js_initializer(self):
return "ImageGallery"
@property
def media(self):
return forms.Media(
js=['app/js/admin/image-gallery.js']
)
# image-gallery.js
function ImageGallery(prefix) {
// Set up the Image Gallery block's custom form behavior...
}
This make the ImageGallery()
function run, but did not run the ListBlock
's initializer, so none of its buttons worked.
On the wagtail issue queue, I was recommended to try something like this:
def js_initializer(self):
initializer_js = super(HeadingBlock, self).js_initializer()
my_custom_js = 'ImageGallery("%s")' % self.definition_prefix
if initializer_js:
# child blocks have custom JS initializers and need to be used
return '%s,\n%s' % (initializer_js, my_custom_js)
return my_custom_js
# image-gallery.js
function ImageGallery(prefix) {
var init_image_gallery = function(element_prefix) {
// Do stuff...
};
return init_image_gallery;
}
I had to make a few improvements to the original suggestion to get the ImageGallery()
part to function, but it still doesn't run the ListBlock
initializer.
Here's what the initializer code that gets generated for ImageGalleryBlock
looks like:
{
'name': ('ImageGalleryBlock'),
'initializer': (StructBlock({
'images': (ListBlock({
'definitionPrefix': ('blockdef-63')
}))
}),
ImageGallery("blockdef-91"))
},
I get the feeling that what I actually need to do is add another key to the dict being passed into StructBlock
, but I haven't got the foggiest clue how.