Hi,
I have a simple / small question:
I have an ItemPresenter (connected to an ItemView). And every View has
3 Buttons. I can have a single ClickHandler inside the View which can
tell me which of my 3 Buttons was clicked:
if(event.getSource() == button1) {
Window.alert("1 was clicked");
} else if(event.getSource() == button2) {
...
That works fine. But now I think I have to go one step further,
because there's not just one instance of that Presenter and View: As
the name ItemPresenter should tell: There's one instance per item. So
I have about 100 of them. And then I have 100 ClickHandlers running.
I'd like to reduce it to one ClickHandler, but I don't know which is
the best way.
One thing I've tested:
public ItemDisplay() {
this.addDomHandler(clickHandler, ClickEvent.getType());
}
public static ClickHandler clickHandler = new ClickHandler() {
public void onClick(ClickEvent event) {
ItemDisplay item = (ItemDisplay) event.getSource();
}
}
Now, if anything of the display was clicked I get a reference to that
display. So far so good. And with:
EventTarget target = event.getNativeEvent().getEventTarget();
I get a reference to the part of the Ui that was clicked. And here
comes the problem:
If I do 'System.out.println(target);' I can see '<button type="button"
class="gwt-Button" style="">button 1</button>' (or Button 2 or 3). But
how can I determine in that static context of the ClickHandler which
of the buttons it was?
Should I create getters for all buttons and check using them? like
this:
@UiField public Button button1;
@UiField public Button button2;
@UiField public Button button3;
public ItemDisplay() {
this.addDomHandler(clickHandler, ClickEvent.getType());
}
public static ClickHandler clickHandler = new ClickHandler() {
public void onClick(ClickEvent event) {
ItemDisplay item = (ItemDisplay) event.getSource();
EventTarget target = event.getNativeEvent().getEventTarget();
if(target.cast() == item.button1.getElement())
{ item.getPresenter().doSth1(); }
else if(target.cast() == item.button2.getElement())
{ item.getPresenter().doSth2(); }
else if(target.cast() == item.button3.getElement())
{ item.getPresenter().doSth3(); }
}
}
Is that okay concerning MVP (supervising controller) ? Or maybe
there's a better solution handling all those handlers?
Thanks for your help :)
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.