如何在GWT中的小部件之间共享数据资源

时间:2012-05-29 11:33:33

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

我正在使用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;
}

}

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

谢谢。

1 个答案:

答案 0 :(得分:1)

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

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

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

  3. 我认为这两种解决方案都可以使用。 解决方案一创建了与您的小部件更紧密的耦合,但更容易实现,我认为如果您只有少量的小部件,您应该采取这种方式 与数据。

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