Skip to content

Commit d6438fb

Browse files
committed
Call complete, rendered, and parent_rendered hooks on render.
1 parent 22bbca3 commit d6438fb

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

django_unicorn/components/unicorn_template_response.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def render(self):
9696

9797
rendered_template = UnicornTemplateResponse._desoupify(soup)
9898
rendered_template = mark_safe(rendered_template)
99+
self.component.rendered(rendered_template)
99100

100101
response.content = rendered_template
101102

django_unicorn/components/unicorn_view.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def construct_component(
115115

116116
component.mount()
117117
component.hydrate()
118+
component.complete()
118119
component._validate_called = False
119120

120121
return component
@@ -235,7 +236,7 @@ def hydrate(self):
235236

236237
def complete(self):
237238
"""
238-
Hook that gets called when the component's methods are all called.
239+
Hook that gets called after all component methods are executed.
239240
"""
240241
pass
241242

@@ -658,6 +659,8 @@ def _get_component_class(
658659
return component_class
659660

660661
if use_cache and component_id in constructed_views_cache:
662+
# Note that `hydrate()` and `complete` don't need to be called here
663+
# because this path only happens for re-rendering from the view
661664
component = constructed_views_cache[component_id]
662665
component.setup(request)
663666
component._validate_called = False

django_unicorn/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ def _process_component_request(
434434
else:
435435
raise UnicornViewError(f"Unknown action_type '{action_type}'")
436436

437+
component.complete()
438+
437439
# Re-load frontend context variables to deal with non-serializable properties
438440
component_request.data = orjson.loads(component.get_frontend_context_variables())
439441

@@ -454,6 +456,8 @@ def _process_component_request(
454456
component.validate(model_names=list(updated_data.keys()))
455457

456458
rendered_component = component.render()
459+
component.rendered(rendered_component)
460+
457461
partial_doms = []
458462

459463
if partials and all(partials):
@@ -540,6 +544,7 @@ def _process_component_request(
540544

541545
if not partial_doms:
542546
parent_dom = parent_component.render()
547+
component.parent_rendered(parent_dom)
543548

544549
parent.update(
545550
{

0 commit comments

Comments
 (0)