GWTP。如何从一个演示者访问另一个演示者 - 他们没有通过插槽机制连接

时间:2016-03-03 14:50:32

标签: gwt gwtp gwt-platform presenter

我有一个用于登录屏幕目的的演示者。我有整个应用程序的AppPresenter。每个视图(登录除外)都显示在AppView插槽中。

在AppView中我有注销按钮,该按钮仅在用户登录时才可见,因此当用户登录时,该按钮将变为可见。当用户点击它注销时,该按钮应该消失。

隐藏它很简单,因为它发生在一个视图/演示者中。但是我在用户登录后显示此按钮时遇到问题,导致它发生在其他演示者/视图上。

我是gwtp的新手,所以请帮我解决这个问题。

如何从登录帐户访问其他演示者?他们没有关系!如果我只使用use App\Classes\className;注释添加AppPresenterLoginPresenter构造函数的类,那会是否足够好?如下:

@Inject

主持人:

@Inject
LoginPresenter(EventBus eventBus, MyView view, MyProxy proxy, CurrentUser currentUser, PlaceManager placeManager, AppPresenter appPresenter) {
    super(eventBus, view, proxy, RevealType.Root);
    this.currentUser = currentUser;
    if (!currentUser.isLoggedIn()) {
        placeManager.revealDefaultPlace();
    }
    this.placeManager = placeManager;
    this.appPresenter = appPresenter;
    getView().setUiHandlers(this);
    Window.setTitle(Routing.Name.login);
    editorDriver = getView().createEditorDriver();
    editorDriver.edit(model);
}



public class AppModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
    install(new UiModule());

    // Application Presenters

    bindPresenter(AppPresenter.class, AppPresenter.MyView.class, AppView.class, AppPresenter.MyProxy.class);
    bindPresenter(HomePresenter.class, HomePresenter.MyView.class, HomeView.class, HomePresenter.MyProxy.class);
    bindPresenter(ErrorPresenter.class, ErrorPresenter.MyView.class, ErrorView.class, ErrorPresenter.MyProxy.class);
    bindPresenter(TestPresenter.class, TestPresenter.MyView.class, TestView.class, TestPresenter.MyProxy.class);
    bindPresenter(PagePresenter.class, PagePresenter.MyView.class, PageView.class, PagePresenter.MyProxy.class);
    bindPresenter(SettingsPresenter.class, SettingsPresenter.MyView.class, SettingsView.class, SettingsPresenter.MyProxy.class);
    bindPresenter(AdminAreaPresenter.class, AdminAreaPresenter.MyView.class, AdminAreaView.class, AdminAreaPresenter.MyProxy.class);

    bindPresenter(LoginPresenter.class, LoginPresenter.MyView.class, LoginView.class, LoginPresenter.MyProxy.class);

    }
}
package pl.daniel.cms.client.place.app;

import gwt.material.design.client.ui.MaterialToast;

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;

import pl.daniel.cms.client.place.Routing;
import pl.daniel.cms.client.security.CurrentUser;
import pl.daniel.cms.client.security.CurrentUserChangedEvent;
import pl.daniel.cms.client.security.CurrentUserChangedHandler;
import pl.daniel.cms.client.service.LoginService;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.ChangeTabHandler;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.RequestTabsHandler;
import com.gwtplatform.mvp.client.TabContainerPresenter;
import com.gwtplatform.mvp.client.TabView;
import com.gwtplatform.mvp.client.annotations.ChangeTab;
import com.gwtplatform.mvp.client.annotations.ProxyEvent;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.annotations.RequestTabs;
import com.gwtplatform.mvp.client.presenter.slots.NestedSlot;
import com.gwtplatform.mvp.client.proxy.AsyncCallFailEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallFailHandler;
import com.gwtplatform.mvp.client.proxy.AsyncCallStartEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallStartHandler;
import com.gwtplatform.mvp.client.proxy.AsyncCallSucceedEvent;
import com.gwtplatform.mvp.client.proxy.AsyncCallSucceedHandler;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;

/**
 * The main {@link com.gwtplatform.mvp.client.Presenter} of the application. It
 * contains a number of tabs allowing access to the various parts of the
 * application. Tabs are refreshed whenever the current user's privileges change
 * in order to hide areas that cannot be accessed.
 */
public class AppPresenter extends TabContainerPresenter<AppPresenter.MyView, AppPresenter.MyProxy> implements AppUiHandlers, CurrentUserChangedHandler, AsyncCallStartHandler, AsyncCallFailHandler,
    AsyncCallSucceedHandler {
    /**
     * {@link AppPresenter}'s proxy.
     */
    @ProxyStandard
    public interface MyProxy extends Proxy<AppPresenter> {
    }

    /**
     * {@link AppPresenter}'s view.
     */
    public interface MyView extends TabView, HasUiHandlers<AppUiHandlers> {
    void refreshTabs();

    void setTopMessage(String string);

    void setLoginButtonVisbility(boolean isVisible);
    }

    /**
     * This will be the event sent to our "unknown" child presenters, in order
     * for them to register their tabs.
     */
    @RequestTabs
    public static final Type<RequestTabsHandler> SLOT_REQUEST_TABS = new Type<>();

    /**
     * Fired by child proxie's when their tab content is changed.
     */
    @ChangeTab
    public static final Type<ChangeTabHandler> SLOT_CHANGE_TAB = new Type<>();

    /**
     * Use this in leaf presenters, inside their {@link #revealInParent} method.
     */
    public static final NestedSlot SLOT_TAB_CONTENT = new NestedSlot();

    private static final LoginService service = GWT.create(LoginService.class);
    private final PlaceManager placeManager;
    private final CurrentUser currentUser;

    @Inject
    AppPresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager, CurrentUser currentUser) {
    super(eventBus, view, proxy, SLOT_TAB_CONTENT, SLOT_REQUEST_TABS, SLOT_CHANGE_TAB, RevealType.Root);
    this.placeManager = placeManager;
    this.currentUser = currentUser;
    getView().setUiHandlers(this);
    onStart();
    }

    protected void onStart() {
    service.isCurrentUserLoggedIn(new MethodCallback<Boolean>() {
        @Override
        public void onFailure(Method method, Throwable exception) {
        MaterialToast.fireToast("Fail to check is current user logged in " + method + " " + exception.getLocalizedMessage());
        }

        @Override
        public void onSuccess(Method method, Boolean response) {
        GWT.log(response.toString());
        // MaterialToast.fireToast("User login status: " + method + " "
        // + response.toString());
        currentUser.setLoggedIn(response);
        getView().setLoginButtonVisbility(response);
        }
    });
    };

    @ProxyEvent
    @Override
    public void onCurrentUserChanged(CurrentUserChangedEvent event) {
    getView().refreshTabs();
    }

    @ProxyEvent
    @Override
    public void onAsyncCallStart(AsyncCallStartEvent event) {
    getView().setTopMessage("Loading...");
    }

    @ProxyEvent
    @Override
    public void onAsyncCallFail(AsyncCallFailEvent event) {
    getView().setTopMessage("Oops, something went wrong...");
    }

    @ProxyEvent
    @Override
    public void onAsyncCallSucceed(AsyncCallSucceedEvent event) {
    getView().setTopMessage(null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * pl.daniel.cms.client.place.app.AppUiHandlers#onLogoutButtonClick()
     */
    @Override
    public void onLogoutButtonClick() {
    service.logout(new MethodCallback<Void>() {

        @Override
        public void onFailure(Method method, Throwable exception) {
        MaterialToast.fireToast("Fail to logout " + method + " " + exception.getLocalizedMessage());

        }

        @Override
        public void onSuccess(Method method, Void response) {
        MaterialToast.fireToast("Succefully logout " + method + " " + response);
        PlaceRequest request = new PlaceRequest.Builder(placeManager.getCurrentPlaceRequest()).nameToken(Routing.Url.login).build();
        placeManager.revealPlace(request);
        currentUser.setLoggedIn(false);
        getView().setLoginButtonVisbility(false);
        }

    });

    }
}

修改

package pl.daniel.cms.client.place.login; import gwt.material.design.client.ui.MaterialToast; import java.util.ArrayList; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import org.fusesource.restygwt.client.Method; import org.fusesource.restygwt.client.MethodCallback; import pl.daniel.cms.client.editor.helper.BeanEditView; import pl.daniel.cms.client.place.Routing; import pl.daniel.cms.client.place.app.AppPresenter; import pl.daniel.cms.client.security.CurrentUser; import pl.daniel.cms.client.service.LoginService; import pl.daniel.cms.shared.model.UserLoginModel; import com.google.gwt.core.shared.GWT; import com.google.gwt.editor.client.EditorError; import com.google.gwt.editor.client.SimpleBeanEditorDriver; import com.google.gwt.event.shared.GwtEvent.Type; import com.google.gwt.user.client.Window; import com.google.inject.Inject; import com.google.web.bindery.event.shared.EventBus; import com.gwtplatform.mvp.client.HasUiHandlers; import com.gwtplatform.mvp.client.Presenter; import com.gwtplatform.mvp.client.annotations.NameToken; import com.gwtplatform.mvp.client.annotations.ProxyStandard; import com.gwtplatform.mvp.client.annotations.TabInfo; import com.gwtplatform.mvp.client.proxy.PlaceManager; import com.gwtplatform.mvp.client.proxy.RevealContentHandler; import com.gwtplatform.mvp.client.proxy.TabContentProxyPlace; public class LoginPresenter extends Presenter<LoginPresenter.MyView, LoginPresenter.MyProxy> implements LoginUiHandlers { @ProxyStandard @NameToken(Routing.Url.login) @TabInfo(container = AppPresenter.class, label = Routing.Name.login, priority = 2) public interface MyProxy extends TabContentProxyPlace<LoginPresenter> { } public interface MyView extends BeanEditView<UserLoginModel>, HasUiHandlers<LoginUiHandlers> { } public static final Type<RevealContentHandler<?>> SLOT_Login = new Type<RevealContentHandler<?>>(); // Editor private SimpleBeanEditorDriver<UserLoginModel, ?> editorDriver; private static final LoginService service = GWT.create(LoginService.class); private UserLoginModel model = new UserLoginModel(); private final CurrentUser currentUser; private final PlaceManager placeManager; private final AppPresenter appPresenter; @Override public void onLoginButtonClick() { if (editorDriver.isDirty()) { model = editorDriver.flush(); validateModel(); if (editorDriver.hasErrors()) { MaterialToast.fireToast("Errors occur"); for (EditorError e : editorDriver.getErrors()) { if (e.getAbsolutePath().equals("")) { MaterialToast.fireToast(e.getMessage() + " " + e.getPath() + " " + e.getAbsolutePath()); } } } else { service.login(model.getEmail(), model.getPassword(), new MethodCallback<Void>() { @Override public void onSuccess(Method method, Void response) { MaterialToast.fireToast("Succefully set info. status code: " + response); // PlaceRequest request = new // PlaceRequest.Builder(placeManager.getCurrentPlaceRequest()).nameToken(Routing.Url.test).build(); // placeManager.revealPlace(request); // placeManager.revealCurrentPlace(); onToggleLogginButtonClick(); } @Override public void onFailure(Method method, Throwable exception) { MaterialToast.fireToast(exception.getLocalizedMessage()); } }); // service.login(model, new MethodCallback<Void>() { // @Override // public void onSuccess(Method method, Integer response) { // MaterialToast.fireToast("Succefully set info. status code: " // + response); // // PlaceRequest request = new // // // PlaceRequest.Builder(placeManager.getCurrentPlaceRequest()).nameToken(Routing.Url.test).build(); // // placeManager.revealPlace(request); // placeManager.revealCurrentPlace(); // // } // // @Override // public void onFailure(Method method, Throwable exception) { // MaterialToast.fireToast(exception.getLocalizedMessage()); // } // }); } } else { MaterialToast.fireToast("Data has not changed"); } } private void validateModel() { Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); Set<ConstraintViolation<UserLoginModel>> violations = validator.validate(model); // model.validate(); GWT.log(String.valueOf(violations.size())); if (violations.size() > 0) { editorDriver.setConstraintViolations(new ArrayList<ConstraintViolation<?>>(violations)); } } @Inject LoginPresenter(EventBus eventBus, MyView view, MyProxy proxy, CurrentUser currentUser, PlaceManager placeManager, AppPresenter appPresenter) { super(eventBus, view, proxy, RevealType.Root); this.currentUser = currentUser; if (!currentUser.isLoggedIn()) { placeManager.revealDefaultPlace(); } this.placeManager = placeManager; this.appPresenter = appPresenter; getView().setUiHandlers(this); Window.setTitle(Routing.Name.login); editorDriver = getView().createEditorDriver(); editorDriver.edit(model); } public enum EditorMode { VIEW, EDIT, CREATE } @Override protected void onReveal() { GWT.log("reveal"); if (this.currentUser.isLoggedIn()) { MaterialToast.fireToast("User already logged in. Redirecting to home page"); placeManager.revealDefaultPlace(); } } @Override protected void onReset() { GWT.log("reset"); } @Override public void onToggleLogginButtonClick() { this.currentUser.switchLoggedIn(); appPresenter.getView().setLoginButtonVisbility(true); // TODO if current place is null redirect do default GWT.log("\\\\ " + placeManager.getCurrentPlaceRequest().getNameToken()); placeManager.revealCurrentPlace(); }; } 更改为:

后出现错误
AppModule
public class AppModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
    install(new UiModule());

    // Application Presenters
    bindSingletonPresenterWidget(AppPresenter.class, AppPresenter.MyView.class, AppView.class);
    // bindPresenter(AppPresenter.class, AppPresenter.MyView.class, AppView.class, AppPresenter.MyProxy.class);

}
}

1 个答案:

答案 0 :(得分:1)

你是绝对正确的:在构造函数中或在字段顶部使用@Inject是一种方法。请确保AppPresenter绑定为singleton

public class ApplicationModule extends AbstractPresenterModule {
        @Override
        protected void configure() {
            bindSingletonPresenterWidget(AppPresenter.class, AppPresenter.MyView.class, AppView.class);
        }
}

这种方式每当你在任何地方@Inject AppPresenter时,你最终会引用你需要的确切实例,是的,这是使用松散绑定组件和依赖注入的好处之一。