【问题标题】:How to share data resources between widgets in GWT如何在 GWT 中的小部件之间共享数据资源
【发布时间】:2023-03-13 11:02:01
【问题描述】:

我正在为一个项目使用 GWT 和 AppEngine。我想知道如何在小部件之间共享数据(ArrayList 对象),这样我就可以集中逻辑并减少对服务器的 RPC 调用次数。

我想到了两种方法,但不知道哪个更好:

1) 当我实例化小部件时,我将 ArrayList 对象作为参数传递,虽然我不知道如何做到这一点,因为小部件被实例化了:

  ThisAppShell shell = GWT.create(ThisAppShell.class);    

2) 通过使用类似 eventBus 的机制

http://www.dev-articles.com/article/Gwt-EventBus-(HandlerManager)-the-easy-way-396001

当用户加载应用程序时,在登录过程完成后,我想下载一个应该可用于所有小部件的员工列表。这一切都应该在 onModuleLoad() 方法中完成。我想在启动时下载它们,因为我想实现某种缓存机制。例如,我想要 2 个 ArrayList 实例: - 加载应用程序时填充的 emplListOnStart - emplListChanges,一个数组,用户将从内部小部件中进行修改。

用户完成更改后(他按下“保存”按钮),将比较两个数组,差异将保存在 appengine(通过 RPC)中,并在 emplListOnStart 中更新。

这是 EntryPoint 类的代码:

public class ThisApp implements EntryPoint {
ThisAppShell shell = GWT.create(ThisAppShell.class);
LoginServiceAsync loginService = GWT.create(LoginService.class);

private ArrayList<Employee> emplListOnStart;

private ArrayList<Employee> emplListChanges;

public void onModuleLoad() {
    RootLayoutPanel.get().clear();
    RootLayoutPanel.get().add(shell);
    loginService.isAuthenticated(new AsyncCallback<UserDto>() {

        public void onFailure(Throwable caught) {
            // TODO Auto-generated method stub

        }

        public void onSuccess(UserDto result) {
                             //Here I should load the emplListOnStart list;
        }

    });
    shell.getLogoutLink().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            loginService.logout(new AsyncCallback() {
                public void onFailure(Throwable caught) {

                }

                public void onSuccess(Object result) {
                                             //Here the user will get logged out
                }
            });
            Window.Location.assign("");
        }
    });

   }
}

这是小部件的代码:

public class ThisAppShell extends Composite {

private static ThisAppShellUiBinder uiBinder = GWT
        .create(ThisAppShellUiBinder.class);

interface ThisAppShellUiBinder extends UiBinder<Widget, ThisAppShell> {
}

@UiField
Anchor logout_link;
@UiField
StackLayoutPanel stackLPanel;
@UiField
TabLayoutPanel tabLPanel;

public ThisAppShell() {
    initWidget(uiBinder.createAndBindUi(this));

    initializeWidget();
}

public void initializeWidget() {
    stackLPanel.add(new HTML("Manage empl."), new HTML("Employees"), 30);
    stackLPanel.add(new HTML("Manage Dept."), new HTML("Departments"), 30);

    // Add a home tab
    HTML homeText = new HTML("This is the home tab");
    tabLPanel.add(homeText, "Home");

    // Add a tab
    HTML moreInfo = new HTML("This is the more info tab");
    tabLPanel.add(moreInfo, "More info");

    // Return the content
    tabLPanel.selectTab(0);
}

public Anchor getLogoutLink() {
    return logout_link;
}

}

这可能吗,或者如何才能做得更好?

谢谢。

【问题讨论】:

    标签: google-app-engine gwt rpc data-sharing gwt-widgets


    【解决方案1】:

    我认为有两种方法可以做到:

    1. 在您的小部件上创建一个设置器来设置您的 ArrayList 实例 (setData())。然后,您可以在 loginService 的 onSuccess 方法中调用此函数。

    2. 将全局 EventBus 的单例实例注入您的小部件(使用 gin/guice)并触发包含您的数据的事件。在小部件中,您必须为特定事件附加一个 EventHandler(即LoadEmplListEvent)。

    我认为这两种解决方案都可以使用。 解决方案一与您的小部件建立了更紧密的耦合,但更容易实现,我认为如果您工作的小部件数量很少,您应该采用这条路线 与数据。

    解决方案是一种更简洁的方法,因为它将小部件与其他小部件分离。您在 onSuccess 方法中触发数据事件一次,并且您不关心小部件。
    对数据感兴趣的小部件将确保它们适当地处理事件(通过处理事件)。我想如果您有很多必须处理数据的小部件,那么第二种方法就是要采用的方法。

    【讨论】:

    • 谢谢。第二个选项听起来更好。你能推荐我一些很好的阅读来实现这样的东西吗? (可能是教程,示例)
    • 你可以看看这个 stackoverflow post。这个article 与 GWTP(MVP 框架)有关,但也适用于非 GWTP 项目。这个google io会议视频也在worth看。
    猜你喜欢
    • 2020-04-07
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    相关资源
    最近更新 更多