using SuggestBox with special Oracle and Suggestion - how to keep view "humble"

75 views
Skip to first unread message

tanteanni

unread,
Apr 19, 2011, 7:55:51 AM4/19/11
to Google Web Toolkit
with your (http://groups.google.com/group/google-web-toolkit/
browse_frm/thread/be1e8e363a6f0794/9ceb716e29a5b985) help i learned
how to implement my own SuggestionOracle("AuSuggestOracle") and own
Suggestions("AuMultiWordSuggestion"). In my case the suggestion object
is constructed with a DTO. On a selection event i need this dto (or
some fields of it) to react appropriate.
I implemented a widget containing 3 suggest boxes with this special
oracle and some logic between them. Now i want to apply MVP pattern -
split this widget in presenter and view.
At the moment the presenters display interface look like that:

public interface Display {
HasSelectionHandlers<Suggestion> getFedLand();
HasSelectionHandlers<Suggestion> getCounty();
HasSelectionHandlers<Suggestion> getCommunity();
AuSuggestOracle getFedLandOracle();
AuSuggestOracle getCountyOracle();
AuSuggestOracle getCommunityOracle();
void clearCounty();
void clearCommunity();
void activateForm();
Widget asWidget();
}
the problem is the implicit knowledge about my model in methods
returning "AuSuggestOracle". so my question is how to get the view/
interface "humble". in my case the displayed suggestion-strings are
ambiguous and i need at least the "id" of a selected item to know what
DTObject is selected.

Ben Imp

unread,
Apr 19, 2011, 9:16:31 AM4/19/11
to google-we...@googlegroups.com
I ran into this issue as well - knowledge of the domain in the view.  I ended up getting around it by creating a delegate for the suggest oracle.

public class SuggestOracleDelegator extends SuggestOracle {

    private SuggestOracle delegate = null;
   
    public SuggestOracleDelegator() {    }
   
   
    public void requestSuggestions(Request request, Callback callback) {
        if(this.delegate != null){
            this.delegate.requestSuggestions(request, callback);
        }
    }

    public void setDelegate(SuggestOracle delegate) {
        this.delegate = delegate;
    }
}

Then I wrap this in a HasSuggestions interface that lets you set the delegate oracle from outside, and attach listeners and so forth.  Seems to work quite well.

-Ben

tanteanni

unread,
Apr 26, 2011, 1:50:00 AM4/26/11
to Google Web Toolkit
thx fo the tip (in particular the idea with "HasSuggestion"
interface).

but in long run i fear i 'll end up with many such interfaces and
delegates to obey to some pattern :-|

Ben Imp

unread,
Apr 26, 2011, 11:04:31 AM4/26/11
to google-we...@googlegroups.com
Most of the GWT widgets have predefined interfaces that will work just fine.  HasValue, HasText, HasClickHandlers, etc.  I rarely bother to extend these.  Ideally, you wont need to generate too many of these extra interfaces.  I think the only additional interface I have created is HasClickHandlersAndEnableable, which lets me have greater control over buttons without adding more methods to the main view interface.

-Ben

tanteanni

unread,
Apr 27, 2011, 3:09:18 AM4/27/11
to google-we...@googlegroups.com
thx for this hint.
the problem in my case is the DTO or the 1:n relationship between id and ambiguous names. i need every text/name keep bound to its id within the suggestion box. Probably that could be achieved by using HasValue together with HasText?? One solution that came to my mind is to provide a constructor that take all 3 oracles. but this only makes the interface more humble?! the view itself still needs the special kind of oracle to work properly.
Reply all
Reply to author
Forward
0 new messages