Skip to content
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

feat: reimplement @RouteScoped to keep beans for PreserveOnRefresh #370

Merged
merged 4 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.vaadin.cdi.itest.routecontext;

import com.vaadin.flow.component.html.Div;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.vaadin.flow.component.html.Div;

public abstract class AbstractCountedView extends Div implements CountedPerUI {

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,60 @@

package com.vaadin.cdi.itest.routecontext;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.ErrorParameter;
import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.router.ParentLayout;
import com.vaadin.flow.router.RouterLink;

import javax.servlet.http.HttpServletResponse;

@RouteScoped
@RouteScopeOwner(ErrorParentView.class)
@ParentLayout(ErrorParentView.class)
public class ErrorHandlerView extends AbstractCountedView
implements HasErrorParameter<NullPointerException> {
implements HasErrorParameter<CustomException> {

public static final String PARENT = "parent";

@Inject
@RouteScopeOwner(ErrorHandlerView.class)
private Instance<CustomExceptionSubButton> buttonInjection;

@Inject
@RouteScopeOwner(ErrorHandlerView.class)
private Instance<CustomExceptionSubDiv> divInjection;

private boolean isSubDiv;

private Component current;

@Override
public int setErrorParameter(BeforeEnterEvent event,
ErrorParameter<NullPointerException> parameter) {
add(
new RouterLink(PARENT, ErrorParentView.class)
);
ErrorParameter<CustomException> parameter) {
add(new RouterLink(PARENT, ErrorParentView.class));

NativeButton button = new NativeButton("switch content", ev -> {
remove(current);
if (isSubDiv) {
current = buttonInjection.get();
} else {
current = divInjection.get();
}
add(current);
isSubDiv = !isSubDiv;
});
button.setId("switch-content");
add(button);
current = buttonInjection.get();
add(current);

return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

package com.vaadin.cdi.itest.routecontext;

import javax.annotation.PostConstruct;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RouterLink;

import javax.annotation.PostConstruct;

@RouteScoped
@RouteScopeOwner(ErrorParentView.class)
@Route("error-layout")
public class ErrorParentView extends AbstractCountedView
implements RouterLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

@RouteScoped
@Route("error")
public class ErrorView extends AbstractCountedView implements BeforeEnterObserver {
public class ErrorView extends AbstractCountedView
implements BeforeEnterObserver {

@Override
public void beforeEnter(BeforeEnterEvent event) {
if (true) {
throw new NullPointerException();
throw new CustomException();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.vaadin.cdi.itest.routecontext;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.component.html.Div;
Expand All @@ -27,9 +30,6 @@
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RouterLink;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

@RouteScoped
@Route("")
@RoutePrefix("master")
Expand All @@ -44,31 +44,20 @@ public class MasterView extends AbstractCountedView
@Inject
@RouteScopeOwner(MasterView.class)
private AssignedBean assignedBean;
@Inject
@RouteScopeOwner(DetailApartView.class)
private ApartBean apartBean;
private Label assignedLabel;
private Label apartLabel;

@PostConstruct
private void init() {
assignedLabel = new Label();
assignedLabel.setId(ASSIGNED_BEAN_LABEL);
apartLabel = new Label();
apartLabel.setId(APART_BEAN_LABEL);
add(
new Label("MASTER"),
new Div(assignedLabel),
new Div(apartLabel),
add(new Label("MASTER"), new Div(assignedLabel),
new Div(new RouterLink(ASSIGNED, DetailAssignedView.class)),
new Div(new RouterLink(APART, DetailApartView.class))
);
new Div(new RouterLink(APART, DetailApartView.class)));
}

@Override
public void afterNavigation(AfterNavigationEvent event) {
assignedLabel.setText(assignedBean.getData());
apartLabel.setText(apartBean.getData());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,32 @@

package com.vaadin.cdi.itest.routecontext;

import javax.annotation.PostConstruct;

import com.vaadin.cdi.annotation.RouteScoped;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLink;

import javax.annotation.PostConstruct;

@Route("")
@Route(value = "", layout = MainLayout.class)
@RouteScoped
public class RootView extends AbstractCountedView {

public static final String MASTER = "master";
public static final String REROUTE = "reroute";
public static final String POSTPONE = "postpone";
public static final String UIID = "UIID";
public static final String EVENT = "event";
public static final String ERROR = "ERROR";

@PostConstruct
private void init() {
Label uiIdLabel = new Label(UI.getCurrent().getUIId() + "");
uiIdLabel.setId(UIID);
add(
new Div(uiIdLabel),
new Div(new Label("ROOT")),
add(new Div(new Label("ROOT")),
new Div(new RouterLink(MASTER, MasterView.class)),
new Div(new RouterLink(REROUTE, RerouteView.class)),
new Div(new RouterLink(POSTPONE, PostponeView.class)),
new Div(new RouterLink(EVENT, EventView.class)),
new Div(new RouterLink(ERROR, ErrorView.class))
);
new Div(new RouterLink(ERROR, ErrorView.class)));
}

}
Loading