Added ability to defer component template setting until after construction.
This commit is contained in:
parent
3362011a8a
commit
3be4761541
@ -21,19 +21,17 @@ public abstract class AbstractViewComponent implements ViewComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private final ComponentTemplate template;
|
||||
private ComponentTemplate componentTemplate;
|
||||
private ComponentContext context;
|
||||
|
||||
public AbstractViewComponent() {
|
||||
this.template = null;
|
||||
}
|
||||
public AbstractViewComponent() {}
|
||||
|
||||
public AbstractViewComponent(ComponentTemplate template) {
|
||||
this.template = template;
|
||||
public AbstractViewComponent(ComponentTemplate componentTemplate) {
|
||||
this.componentTemplate = componentTemplate;
|
||||
}
|
||||
|
||||
public AbstractViewComponent(Class<? extends ComponentTemplate> templateClass) {
|
||||
this.template = instantiateTemplate(templateClass);
|
||||
this.componentTemplate = instantiateTemplate(templateClass);
|
||||
}
|
||||
|
||||
public AbstractViewComponent(
|
||||
@ -46,7 +44,17 @@ public abstract class AbstractViewComponent implements ViewComponent {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
final var templateClass = templateClassFactory.getTemplateClass(compileResult);
|
||||
this.template = instantiateTemplate(templateClass);
|
||||
this.componentTemplate = instantiateTemplate(templateClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComponentTemplate getComponentTemplate() {
|
||||
return this.componentTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComponentTemplate(ComponentTemplate componentTemplate) {
|
||||
this.componentTemplate = componentTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -59,10 +67,6 @@ public abstract class AbstractViewComponent implements ViewComponent {
|
||||
return Objects.requireNonNull(this.context);
|
||||
}
|
||||
|
||||
protected ComponentTemplate getTemplate() {
|
||||
return Objects.requireNonNull(template);
|
||||
}
|
||||
|
||||
protected void beforeRender() {}
|
||||
|
||||
protected void afterRender() {}
|
||||
@ -76,7 +80,7 @@ public abstract class AbstractViewComponent implements ViewComponent {
|
||||
*/
|
||||
@Override
|
||||
public void renderTo(Writer out) throws IOException {
|
||||
final Closure<?> closure = this.getTemplate().getRenderer();
|
||||
final Closure<?> closure = this.getComponentTemplate().getRenderer();
|
||||
closure.setDelegate(this);
|
||||
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
|
||||
this.beforeRender();
|
||||
|
@ -5,6 +5,9 @@ import groowt.view.component.context.ComponentContext;
|
||||
|
||||
public interface ViewComponent extends View {
|
||||
|
||||
ComponentTemplate getComponentTemplate();
|
||||
void setComponentTemplate(ComponentTemplate componentTemplate);
|
||||
|
||||
/**
|
||||
* <em>Note:</em> compiled templates are required to automatically
|
||||
* call this method after the component is constructed. One
|
||||
|
@ -89,7 +89,7 @@ public abstract class AbstractWebViewComponent extends AbstractViewComponent imp
|
||||
@Override
|
||||
public void renderTo(Writer out) throws IOException {
|
||||
final ComponentWriter webWriter = new DefaultComponentWriter(out);
|
||||
final Closure<?> renderer = this.getTemplate().getRenderer();
|
||||
final Closure<?> renderer = this.getComponentTemplate().getRenderer();
|
||||
renderer.setDelegate(this);
|
||||
renderer.setResolveStrategy(Closure.DELEGATE_FIRST);
|
||||
this.beforeRender();
|
||||
|
@ -1,5 +1,6 @@
|
||||
package groowt.view.component.web.compiler;
|
||||
|
||||
import groowt.view.component.ComponentTemplate;
|
||||
import groowt.view.component.context.ComponentContext;
|
||||
import groowt.view.component.runtime.ComponentWriter;
|
||||
import groowt.view.component.web.WebViewComponent;
|
||||
@ -11,6 +12,16 @@ import java.util.List;
|
||||
@ApiStatus.Internal
|
||||
public final class AnonymousWebViewComponent implements WebViewComponent {
|
||||
|
||||
@Override
|
||||
public ComponentTemplate getComponentTemplate() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComponentTemplate(ComponentTemplate componentTemplate) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(ComponentContext context) {
|
||||
throw new UnsupportedOperationException();
|
||||
|
Loading…
Reference in New Issue
Block a user