-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to cleanly extend media definitions when subclassing an adapter #5
Comments
Django has a nice-in-theory mechanism for inheritance when defining media through an inner We can't use Django's implementation directly, for two reasons:
(on Django 3.0.14)
We might expect the ChooserWidget -> ImageChooserWidget inheritance to imply that image-chooser.js depends on chooser.js, but it doesn't - it just uses the ordinary 'merge' operation on the two media objects, which requires both sides to specify their dependencies explicitly. In other words, ImageChooserWidget's media has to explicitly include chooser.js, which rules out the kind of 'black box' treatment of ChooserWidget that telepath's way of thinking really demands: "ImageChooserWidget's media consists of whatever ChooserWidget needs, plus image-chooser.js". I think this is a misfeature on Django's side: a naive merge algorithm that just repeatedly runs "if item not in result: result.append(item)" would be better on all counts except dubious edge cases like [A, B, D] + [A, C, D] having to equal [A, B, C, D] and not [A, B, D, C]. Given that telepath doesn't have the obligation that Django has to preserve backward compatibility here, I reckon that reinventing the wheel and rolling our own alternative is the right technical approach :-) |
Following the changes in #4, where the favoured way of including media is now to call |
The discussion at wagtail/wagtail#7094 (comment) shows a few iterations on extending one of the existing adapters supplied with Wagtail, on both the Python and Javascript side. This means appending the .js file containing the new code to the media definition on the parent, and the natural way to do that would be:
but adding two media objects together like that doesn't guarantee ordering (because it performs more complex dependency resolution, and we haven't specified that StructBlockAdapter's media is a dependency of image-block.js). We ended up having to either repeat the parent's js, or poke inside its internal _js property.
The solution is probably to keep those definitions as plain lists for as long as possible, add a bit of (metaclass?) hackery so that we append to those lists on subclassing, and only turn them into Media objects at the point where telepath retrieves the
media
property.The text was updated successfully, but these errors were encountered: