thanks for the code,
I'm actually trying to avoid the anonymous class code:
getDisplay().setOnCityNameStartChangeCallback(new
SimpleCallback<Object>() {
//...code to run whenever the CityNameStart field changes
}
and rather have simple member methods be invoked like:
public void onCityNameStartFieldChange() {
....
}
that gets directly invoked by the View.
I'm kinda getting sick of writing anonymous classes.
The approach you are suggesting is quite similar to just making
anonymous ClickHanders or ValueChangeHandler instances.
I'd much rather define higher level events.
So, if I had a view that had a few fields:
@UiField Button saveButton;
@UiField Button cancelButton;
@UiField Button addEmailButton;
I'd like to have some relevant api defined for ui events that is
business logic agnostic, so something like this:
public interface BeanHandler {
public void saveClicked();
public void cancelClicked();
public void emailAdded();
}
etc. perhaps these methods could have parameters, like some event
that had contextual data (like what view class fired the event), but
basically this boils down to this:
@UiHandler("saveButton")
public void onSaveClicked(ClickEvent event) {
beanHandler.saveClicked();
}
@UiHandler("cancelButton")
public void onCancelClicked(ClickEvent event) {
beanHandler.cancelClicked();
}
@UiHandler("addEmailButton")
public void onAddEmailClicked(ClickEvent event) {
beanHandler.emailAdded();
}
and of course, beanHandler is just some instance of an interface that
gets injected via a setter or something.
then your presenter turns into this:
public class BeanPresenter implments BeanHandler {
..... business logic methods...
// BeanHandler methods:
public void saveClicked() {
someService.save(myBean);
}
public void cancelClicked() {
getDisplay().reset();
}
public void emailAdded() {
String email = getDisplay().getEmailValue();
myBean.addEmail(email);
}
}
this accomplishes the same things you itemize above, it's just that
you don't have to deal with boiler plate anonymous classes that
(usually) invoke the same kinds of methods I just defined, usually you
have something like this:
ClickHandler handler = new ClickHandler() {
public void onClick(ClickEvent event) {
saveClicked();
}
}
public void saveClicked() {
....
}
I'm leaning towards any approach that cuts out the boiler plate code
above. In essence, your approach and what I have in mind do the same
thing, I just want to avoid the anonymous classes :)
thanks!
-bryce
On Tue, Dec 29, 2009 at 6:47 AM, FKereki <[email protected]> wrote:
> Here goes... I'm doing a simple form that allows you to browse all
> cities whose names start with a given string.
>
> The ui.xml file contains a textbox, a couple of buttons, and a grid;
> the relevant lines are:
>
> <g:HTMLPanel>
> <h1>CitiesUpdater</h1>
> Start of City Name:
> <g:TextBox u:field="cityNameStart"/>
> <g:Button u:field="getCitiesButton" text="Get Cities"/>
> <g:Button u:field="updateCitiesButton" text="Update Cities"/>
> <hr />
> <g:FlexTable u:field="cg"/>
> </g:HTMLPanel>
>
> The View includes:
>
> �...@uitemplate("CitiesUpdaterView.ui.xml")
> interface Binder extends UiBinder<HTMLPanel, CitiesUpdaterView> {}
> private static final Binder binder = GWT.create(Binder.class);
>
> �...@uifield TextBox cityNameStart;
> �...@uifield Button getCitiesButton;
> �...@uifield Button updateCitiesButton;
> �...@uifield FlexTable cg;
>
> SimpleCallback<Object> onGetCitiesClickCallback;
> SimpleCallback<Object> onUpdateCitiesClickCallback;
> SimpleCallback<Object> onCityNameStartChangeCallback;
>
> �...@override
> public void setOnCityNameStartChangeCallback(SimpleCallback<Object>
> acb) {
> onCityNameStartChangeCallback = acb;
> }
>
> �...@override
> public void setOnGetCitiesClickCallback(SimpleCallback<Object> acb)
> {
> onGetCitiesClickCallback = acb;
> }
>
> �...@override
> public void setOnUpdateCitiesClickCallback(SimpleCallback<Object>
> acb) {
> onUpdateCitiesClickCallback = acb;
> }
>
> �...@uihandler("cityNameStart")
> void uiOnChange(ChangeEvent event) {
> onCityNameStartChangeCallback.onSuccess(null);
> }
>
> �...@uihandler("getCitiesButton")
> void uiOnGetClick(ClickEvent event) {
> onGetCitiesClickCallback.onSuccess(null);
> }
>
> �...@uihandler("updateCitiesButton")
> void uiOnUpdateClick(ClickEvent event) {
> onUpdateCitiesClickCallback.onSuccess(null);
> }
>
> The Presenter includes:
>
> public CitiesUpdaterPresenter(
> final String params, final CitiesUpdaterDisplay
> citiesUpdaterDisplay,
> final Environment environment) {
>
> // ...
>
> getDisplay().setOnGetCitiesClickCallback(new SimpleCallback<Object>
> () {
> // ...code to run, when the Get Cities button is clicked
> });
>
> getDisplay().setOnUpdateCitiesClickCallback(new
> SimpleCallback<Object>() {
> // ...code to run when the Update button is clicked
> });
>
> getDisplay().setOnCityNameStartChangeCallback(new
> SimpleCallback<Object>() {
> //...code to run whenever the CityNameStart field changes
> }
>
> So...
>
> * the Presenter is injected with the Display (View) to use
> * the Presenter injects three callbacks into the View
> * the View uses @UiHandler to define three handlers
> * the handlers just call the callbacks that were set by the Presenter
>
> Note that SimpleCallback is an extension of AsyncCallback.
>
> I hope this is clearer; I deleted as much code as possible, to make
> the example simpler. Please ask again if you have any doubts or
> questions.
>
> Best regards,
> F.Kereki
>
> --
>
> 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.
>
>
>
--
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.