Skip to content

BaseElementView

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

When we directly implement ContentView we are implementing the raw interface, which means there will be a lot of wiring that we need to do to make the view works as expected with its presenter, for example, by directly implementing ContentView we have to implement the wiring to tell the presenter when our view attached/detached from the DOM, also how does our view recreate the content when ever it needs re-render.

The BaseElementView is a generic abstract class that implements all this wiring for you, in most of the cases you will need to extend from this class, and implement the init method which should return the root element.

import elemental2.dom.HTMLDivElement;
import org.dominokit.domino.api.client.annotations.UiView;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.domino.view.BaseElementView;
import org.dominokit.samples.shell.client.presenters.BookProxy;
import org.dominokit.samples.shell.client.views.BookView;

@UiView(presentable = BookProxy.class)
public class BookViewImpl extends BaseElementView<HTMLDivElement> implements BookView {

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

    @Override
    protected HTMLDivElement init() {
        return root.element();
    }
}

This view will automatically inform the presenter when ever its root element is attached/detached from the DOM, and will automatically re-drawn when it is requested to be revealed again.

there is only few cases when you can't extend from BaseElementView such as when the view does not have a single root element, like layout that consist of several sections appended to the page body, or for modal views as they should extend from ModalView.