ArcBees / GWTP

A complete model-view-presenter framework to simplify your next GWT project.
Other
335 stars 132 forks source link

More overload of bindSlot() to mitigate possible ambiguous calls #704

Closed Chris-V closed 9 years ago

Chris-V commented 9 years ago

@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).

rdwallis commented 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 in the parent then that slot can represent the collection. So eg removing a child from the slot, removes it from the collection.


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.

rdwallis commented 9 years ago

lgtm btw, very clever.

Chris-V commented 9 years ago

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.

rdwallis commented 9 years ago

@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.