Skip to content

UI handlers

Ahmad K. Bawaneh edited this page Nov 7, 2021 · 1 revision

The view interface is how presenter interact with view without having access to its implementation, Views interact with presenters in the same way, views does not know about presenters implementations and details, therefore we use what is called UiHandlers which is an interface implemented by the presenter and used by the view.

the UiHandlers is a marker interface, to implement UI handlers create an interface that extends the UiHandlers interface, then make your view interface extends from HasUiHandlers interface passing your created UiHandlers interface as a generic type, this will add the method setUihandlers to your view, then the presenter implements the created UiHandlers interface.

When framework creates the presenter instance it will inject the presenter as a UiHandlers in the view using the setUiHandlers method.

Example :

If we have the following view :

public interface BookView extends ContentView, HasUiHandlers<BookView.BookUiHandlers> {
    void setBookTitle(String title);

    interface BookUiHandlers extends UiHandlers {
        void onBookDelete(String title);
    }
}

Then we implement the UiHandlers in the presenter :

@PresenterProxy
@AutoRoute(token = "books/:title")
public class BookProxy extends ViewBaseClientPresenter<BookView> implements BookView.BookUiHandlers {

    @PathParameter
    String title;

    @OnReveal
    public void setBookTitle(){
        view.setBookTitle(title);
    }

    @Override
    public void onBookDelete(String title) {
        //delete book
    }
}

The view implementation will implement HasUiHandlers :

import elemental2.dom.HTMLDivElement;
import org.dominokit.domino.api.client.annotations.UiView;
import org.dominokit.domino.api.shared.extension.Content;
import org.dominokit.domino.ui.button.Button;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.samples.shell.client.presenters.BookProxy;
import org.dominokit.samples.shell.client.views.BookView;

@UiView(presentable = BookProxy.class)
public class BookViewImpl implements BookView {

    private DominoElement<HTMLDivElement> root = DominoElement.div();
    private BookUiHandlers uiHandlers;

    @Override
    public void setBookTitle(String title) {
        root
                .setTextContent(title)
                .appendChild(Button.create("Delete")
                        .addClickListener(evt -> {
                            uiHandlers.onBookDelete(title);
                        }));
    }

    @Override
    public Content getContent() {
        return (Content<HTMLDivElement>) () -> root.element();
    }

    @Override
    public void setUiHandlers(BookUiHandlers uiHandlers) {
        this.uiHandlers = uiHandlers;
    }
}

In @UiView the presentable argument can reference presenters or proxies or a mix.