プロジェクトに GWT と AppEngine を使用しています。ウィジェット間でデータ (ArrayList オブジェクト) を共有する方法を知りたいので、ロジックを集中化し、サーバーへの RPC 呼び出しの数を減らすことができます。
私は2つの方法を考えましたが、どちらが良いかわかりません:
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。
ユーザーが変更を完了した後 (「保存」ボタンを押す)、2 つの配列が比較され、違いが (RPC 経由で) appengine に保存され、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;
}
}
これは可能ですか、またはこれをより良く行うにはどうすればよいですか?
ありがとうございました。