开发者

GWT: how to fire custom events from View and consumed by Activity

I can't make a custom event be received in the Activity. Can anyone please tell me what am I missing? I am using GWT 2.1, MVP pattern and UiBinder.

Here's a sample of what I wrote:

Let's say I have MyCustomEvent class and its handler interface MyCustomEventHandler with its onMyCustomEvent(MyCustomEvent event) method.

I implement the handler interface in the Activity:

    class MyActivity extends AbstractActivity implements MyCustomEventHandler {
    ....

      public void onMyCustomEvent(MyCustomEvent event) {
        doWhatYouKnow();

      }

      //EventBus is injected with GIN
       public void start(AcceptsOneWidget container, EventBus eventBus) {


        ...         
        eventBus.addHandler(MyEvent.TYPE, this);


    }    



}

Now, the sending part in the view:

    public class MyWidget extends Composite {

    final PopUpPanel myPopUp;

        public MyWidget() {
             initWidget(uiBinder.createAndBindUi(this));

             myPopUp.addCloseHandler(new CloseHandler<PopupPanel>() {

            @Override
            publ开发者_C百科ic void onClose(CloseEvent<PopupPanel> event) {

                       MyEvent event = new MyEvent();
               fireEvent(event);

            }

        });

         }

}

No exception are thrown and unfortunately onMyCustomEvent is never called in the MyActivity class. Any idea? Thanks a million.


@MyWidget

you can make the constructor take parameter ( eventBus ) which you can pass this from class MyActivity

so when you fire the event @MyActivity the action will be executed @MyWidget

try this , i think it will work .


I think one of your comments is pointing you in the right direction here. What I'm going to guess is going on is that you have more than one EventBus floating around (there should usually only be one event bus per application).

First of all, make sure the EventBus in your Gin module is bound in the Singleton scope. Also, make sure this is the event bus that you pass in to your PlaceController, and not one you're constructing on your own.

Also, I wouldn't be too worried about the fact that your object is a ResettableEventBus in one place. I believe that's just an object that's created by the Activities/Places framework that just wraps the EventBus object you give it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜