@dominikの回答に基づいて、いくつかの改善を行いました。Gistを参照してください。これには、Modal/PopupViewの実装に使用できるいくつかの抽象基本クラスが含まれています。Presenter
全体をに渡さないため、少し複雑ですが、よりクリーンですView
。モーダルが閉じているときにView
と対話するためのインターフェイスはです。Presenter
HasModalUnbind
これらのクラスは次のように使用します。プレゼンターの例:
public class ErrorModalPresenter extends ModalPopupPresenter<ErrorModalPresenter.MyView> {
public interface MyView extends ModalPopupView {
DivElement getErrorMessage();
}
private final ErrorEvent error;
@Inject
public ErrorModalPresenter(final EventBus eventBus,
final MyView view,
@Assisted final ErrorEvent error) {
super(eventBus, view);
this.error = error;
}
@Override
public void unbindModal() {
ErrorDismissEvent.fire(this, this);
}
@Override
protected void onBind() {
super.onBind();
//noinspection ThrowableResultOfMethodCallIgnored
getView().getErrorMessage().setInnerText(error.getCause().getMessage());
}
}
ビューの例:
public class ErrorModalView extends ModalPopupViewImpl implements ErrorModalPresenter.MyView {
@UiField(provided = true)
Modal errorModal;
@UiField
DivElement errorMessage;
interface Binder extends UiBinder<Widget, ErrorModalView> {}
@Inject
public ErrorModalView(final EventBus eventBus,
final Binder uiBinder) {
super(eventBus);
errorModal = initModal();
initWidget(uiBinder.createAndBindUi(this));
}
@Override
public DivElement getErrorMessage() {
return errorMessage;
}
}
そして、記録のためだけのUiBinder XML:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'
xmlns:b='urn:import:com.github.gwtbootstrap.client.ui'>
<b:Modal ui:field='errorModal' title='Error'>
<g:HTML>
<div ui:field='errorMessage'/>
</g:HTML>
<b:ModalFooter>
<b:Button text='Close' dismiss='MODAL'/>
</b:ModalFooter>
</b:Modal>
</ui:UiBinder>
unbindModal()
のErrorModalPresenter
親プレゼンターがキャッチしたイベントを発生させますErrorModalPresenter
。そこで、モーダルプレゼンターがコンテナーから削除されunbind()
、プレゼンターに呼び出されます。もちろん、他の解決策も可能ですunbindModal()
。
基本クラスは、モーダルが非表示になると削除されるワンショットモーダルであると想定しています。この動作は、のinitModal()で変更できますModalPopupViewImpl
。