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

While reveal is about when to reveal the presenter view, slots are about where to reveal it, a slot in Domino-mvp is a named part of the current view of the application, which can hold one or more elements, presenters can do two things with slots, they can register slots, or they can be revealed in a slot, first we will see how we can register slots using the @RegisterSlots annotation :

  • Registering slots
@PresenterProxy
@AutoRoute
@AutoReveal
@RegisterSlots({"leftPanel", "mainPanel"})
public class LayoutProxy extends ViewBaseClientPresenter<BookView> {

}

In the above example the layoutProxy is registering two slots leftPanel and mainPanel and now it is the job of the presenter view to implement the methods that will create the slots and the presenter will assign those names to the created slots, you can see how we do this if we check on the generated code from such a presenter :

/**
 * This is a generated class, please don't modify
 */
@Presenter(
    name = "",
    parent = ""
)
@AutoRoute(
    token = "",
    routeOnce = false,
    reRouteActivated = false,
    generateTask = true
)
@RoutingTask(LayoutProxy_PresenterHistoryListenerTask.class)
@AutoReveal
public class LayoutProxy_Presenter extends LayoutProxy {
  @Override
  protected SlotsEntries getSlots() {
    SlotsEntries slotsEntries = SlotsEntries.create();
    slotsEntries.add("leftPanel", view.getLeftPanelSlot());
    slotsEntries.add("mainPanel", view.getMainPanelSlot());
    return slotsEntries;
  }
}

Notice the calls to the view that each should return a slot, and in order to avoid any type errors in case we change our registered slots name we also generate an interface that can be extended by the presenter view interface, so will get a compile error if such change happens without changing the implementation :

import org.dominokit.domino.api.client.mvp.slots.IsSlot;

public interface LayoutProxySlots {
  IsSlot<?> getLeftPanelSlot();

  IsSlot<?> getMainPanelSlot();
}

What kind of slot we are registering and what element is assigned to that slot is up to the view to decide, since the presenter does not need to know about UI details, but we need to know that slots can have different types and behaviors.

Slots registered by a presenter can be overridden by another presenter, but this does not mean the original one is actually removed from the slot registry, slots with the same name will be registered in stack and the next presenter that should be revealed on that slot name will pop the slot from the top of the stack, for example, presenter A registered a slot named content then presenter B got activated and should be revealed in slot content so it will use the slot to reveal its view, now presenter C which should be revealed in content got activated and also registered two new slots , leftPanel and content again, now presenter B got activated again wiht out deactivating presenter C, this time presenter B will be revealed in the content slot defined by C instead of content slot registered by A. this give us a lot of flexibility with layouts.

  • Slot type

A slot eventually will be registered with a specific name, but slots are actual classes that can have different behaviors, all slots are implementation of the IsSlot interface, which has one mandatory method to implement updateContent and an optional method cleanUp, the first will be called when the view is being revealed to update the slot content, while the other is called when the presenter is deactivated.

Some pre-defined slots in Domino-mvp that are all related to web browser environment are :

  • SingleElementSlot :
    Extends from the base class ElementSlot and allow only one root element in it, updating the content of this slot with a new element will automatically remove the old content.
  • AppendElementSlot :
    Extends from base class ElementSlot and allows multiple root elements to be added to it, updating the content of such slot will keep the old content and will append the new content after the old content, to clear the slot or to remove and elements from it, we need to explicitly remove them manually.

In addition to those types there is also another slot types that will have an entry registered for them by default :

  • BodyElementSlot :

    Implements the ContentSlot interface and is assigned to the document body element, and can't be assigned to other elements, this slot will allow only one root element in it just like the SingleElementSlot, a slot of this type will be registered by default on application startup under the name body-slot the name of the slot can be accessed as constant through org.dominokit.domino.api.shared.extension.PredefinedSlots#BODY_SLOT.

  • ModalSlot :

    Implements the ContentSlot interface and is not assigned to any element, and it can't be assigned to a specific element, the same slot can be used to show as many pop-ups or modals as we want, a slot of this type will be registered by default on application startup under the name modal-slot the name of the slot can be accessed as constant through org.dominokit.domino.api.shared.extension.PredefinedSlots#MODAL_SLOT.

  • FakeSlot :

    Implements the ContentSlot interface and is only used in testing, we will discuss this slot more when we talk about testing.

    Despite the fact that those slots type are sufficient to implement most of the use cases, the users of Domino-mvp can implement any kind of slot with thier custom logic, for example, a slot that allows N elements to be appended before it starts recycle from the first appended element.

    So far we talked about registering slots and slot types, next is how we tell a presenter in which slot it will be revealed.

  • Revealing in slots :

    To reveal a presenter in a specific slot we use the @Slot annotation, this annotation take one mandatory argument which is the slot name :

    import org.dominokit.domino.api.client.annotations.presenter.Slot;
    
    @PresenterProxy
    @AutoRoute(token = "home")
    @Slot("content")
    @AutoReveal
    public class HomeProxy extends ViewBaseClientPresenter<HomeView> implements HomeView.HomeUiHandlers {
    
    }

    When this presenter is activated it will try to reveal its view in a slot name content, if such slot is not found we will get a runtime error.

Clone this wiki locally