- What are slots?
- How to use them
- Multiple slots in a Presenter
- Slot Types
What are slots?
In GWTP, slots are a feature that facilitate the creation of a hierarchy between presenters. They allow you to nest presenters into each other, allowing for child and parent presenters. A presenter is defined as a “child” when it is “set” into the slot of another presenter. Inversely, a “parent” presenter is defined when a child presenter is set into its slot. Such nesting can be useful when, for example, you want to include a widget into an existing presenter, or even when you want a whole presenter to be the child of another.
How to use them
To use a slot, it must first be declared in the presenter defined as its “parent”. Once you’ve chosen the best slot type for your use case (more on this later), you declare it like so:
public static final SingleSlot SLOT_SIDE_PANEL = new SingleSlot();
Then, assuming you have injected the presenter that will be “child” to the current presenter, you call the
setInSlot(SLOT_SIDE_PANEL, sidePanelPresenter) method. This method will make the
sidePanelPresenter a child of the current presenter. Alternatively, you can call
addToSlot(SLOT, presenter) to append a child to the current children of a presenter. However, make sure that you’ve declared a slot that takes multiple presenters as children.
To display the presenter, a container must be declared. Inside the parent presenter’s view, we declare a field that will act as the container:
@UiField ReplacePanel sidePanelContainer. Finally, we call
bindSlot(ApplicationPresenter.SLOT_SIDE_PANEL, sidePanelContainer) from the parent’s view constructor.
Multiple Slots in a Presenter
There’s no restriction to the number of slots you can declare in a presenter. You can also declare slots with different types without problem. For instance, take a look at the
ApplicationPresenter available slots:
GWTP now provides different types of slots for different uses. The generic Slot is still usable, however you can now choose a slot that is more adapted to a specific situation.
This slot can only take one presenter. This is useful when you want to reserve the slot for specific presenter. For example, this slot is declared in
ProductPresenter. The slot is for the
PricePresenter which holds the product name and price of a specific item for sale.
static SingleSlot SLOT_PRICE = new SingleSlot<>();
The slot is package-private so it can be accessed by the
ProductView but not externally. Calling
getChild(SLOT) will return the child presenter.
A PermanentSlot is the same as a SingleSlot, the only difference being that once a presenter is added, it can never be removed. This means that
getChild(SLOT) will never return null. We used a PermanentSlot for the
CartItemsPresenter which holds individual cart items.
public static final PermanentSlot SLOT_CART_ITEMS = new PermanentSlot<>();
Here we’ve chosen the PermanentSlot instead of the SingleSlot because we don’t want the presenter containing the client’s order to be removed.
This slot is the same as the SingleSlot but it can only take presenters with a proxy. This means that you can’t use PresenterWidgets with this type of slot because they don’t use proxies. In the BeeStore, this slot is used on the ApplicationPresenter which is the root presenter for the application. The NestedSlot is used here so that HomePresenter can be nested into it.
public static final NestedSlot SLOT_MAIN = new NestedSlot();
Other presenters also use this slot such as the
ProductPresenter and the
This slot is the most permissive of all. It can contain multiple presenters or presenter widgets. You can call
getChildren(SLOT) to see which presenters it contains. For instance, this slot is used on the home page of the BeeStore to display the products.
static Slot SLOT_MAIN_PRODUCTS = new Slot<>();
Each product has its own presenter widget created from a factory. Then the widgets are added to the slot on the
A note on Typed Slots
When you need to add different types of presenters to a Slot, you can declare a slot without specifying a generic type. For example, let’s take a look at the
static Slot SLOT_MAIN = new Slot();
If you need to know what presenters the slot contains, you can call
getChildren() which will return a set of all child presenters associated with the slot.
Now that you’ve seen how great slots are for nesting presenters into each other, you can start experimenting with every one of them. Don’t hesitate to take a deeper look at the BeeStore source code, and star the repository if you appreciate it. We did it for you guys!
Feel free to leave feedback or comments in the comment section below.