Closed Chris-V closed 9 years ago
It's helpful when you call getChild / getChildren and get back the exact Presenter that is in the slot.
If you use a specific Presenter class then it does create tighter coupling between presenters and their children but it's optional.
It's not the only thing I use them for but one of the most useful things I've found is that the slot on it's own can be used to represent state of a model. If you create a Slot
So lets say I have a model:
class Book {
List<Chapter> chapters;
}
To represent that model I can create a BookPresenter with a Slot<ChapterPresenter>
.
Then instead of having to keep track of the chapters and their ChapterPresenters separately the slot itself can manage the collection.
So to get the current List of chapters you might have a method in BookPresenter:
List<Chapter> getChapters() {
List<Chapter> chapters = new ArrayList<>();
for (ChapterPresenter chapterPresenter: getChildren(CHAPTER_SLOT)) {
chapters.add(chapterPresenter.getChapter());
}
return chapters;
}
And so now if you have a Delete Chapter
button in your ChapterView all you need to call when that button is clicked is removeFromParentSlot() and your list of chapters will update automatically.
lgtm btw, very clever.
Good explanation (and use case). I'm gonna keep a link to it for the documentation. I'm gonna look at some syntactic sugar to avoid the generic when it's not needed.
@Chris-V this actually doesn't work. If you try to pass SimplePanel into bindSlot you get reference to bindSlot() is ambiguous.
I'm reverting this for now and restoring your branch.
@rdwallis Since I happen to be working with slots, I'm wondering what benefit the generic brings to
IsSlot<? extends PresenterWidget<?>>
. Does it require a type argument because of some technical limitation? The way I see it, it can lead to higher coupling if used with subtypes (not so good). Also, it's more typing (not so much an issue, but can be annoying at times).