首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用选项mvp4g LazyPresenter绑定multiple=true两次

使用选项mvp4g LazyPresenter绑定multiple=true两次
EN

Stack Overflow用户
提问于 2014-06-18 21:10:33
回答 2查看 320关注 0票数 0

我在我的gwt项目中使用mvp4g。对于我的一个演示者,我使用选项multiple=true,并以这种方式创建和绑定演示程序:

代码语言:javascript
复制
ObjectPresenter mainObject = eventBus.addHandler(ObjectPresenter.class, false);
mainObject.setId(id);
mainObject.bind();
view.addWidget(mainObject.getView().asWidget());

ObjectPresenter扩展了LazyPresenter。

当我从eventBus调用由ObjectPresenter捕获的第一个事件时,再次调用LazyPresenter的方法bind()

bind方法在树中有其他方法:createPresenter(); view.createView(); bindView();。在bindViewObjectPresenter方法中,我通过添加下一个小部件来修改视图。因为该方法被调用了两次(一次由我直接调用,一次由框架调用),所以会复制一些小部件。

我对代码进行了调试,并发现当调用来自BaseEventHandler的事件时,调用eventBus中的这一部分代码:

代码语言:javascript
复制
public final boolean isActivated( boolean passive, String eventName, Object... parameters ) {
    boolean activated = this.activated && pass( eventName, parameters );
    if ( activated ) {
        if ( passive ) {
            return binded;
        } else {
            onBeforeEvent();
            if ( !binded ) {
                bind();
                binded = true;
            }
        }
    }
    return activated;
}

在直接调用bind (通过mainObject.bind())之后,BaseEventHandler中的绑定属性没有设置为true,因此在调用第一个事件时再次调用bind方法。

当方法binded (直接调用)完成时,我可以将BaseEventHandler变量设置为ObjectPresenter中的true,但我不确定它是否是正确的方法.

你能告诉我如何处理这个问题吗?

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-22 17:27:13

我没有足够仔细地阅读文档!我使用的是eventBus.addHandler(ObjectPresenter.class, false)方法(带有bind参数),它具有以下java。

代码语言:javascript
复制
     /**
     * Create a new instance of the handler, bind it only if this option is set to true and add it
     * to event bus. If you decide not to bind the handler at creation, you will have either make
     * sure the handler is displayed only after it handles its first method (otherwise the view is
     * not binded so it seems inactive) or call manualy the bind method.<br/>
     * <br/>
     * When binding the handler, you have to call the isActivated method. This method will be called
     * with eventName and parameters set to null.
     * 
     * @param <T>
     *            type of the handler created
     * @param handlerClass
     *            class of the handler to create
     * @param bind
     *            if true, bind the handler at creation, otherwise do nothing.
     * @return new instance of the handler created
     * 
     * @throws Mvp4gException
     *             thrown if the instance of the handler can not be created by the event bus
     */
    <E extends EventBus, T extends EventHandlerInterface<E>> T addHandler( Class<T> handlerClass, boolean bind ) throws Mvp4gException;

我遗漏的部分是,当绑定处理程序时,您必须调用isActivated方法。这个方法将被eventName调用,参数设置为null. --这是我的问题--我没有这么做!

票数 0
EN

Stack Overflow用户

发布于 2014-06-19 14:56:10

首先,问题是,你真的需要multiple=true-featrue吗?此特性设计为一次使用同一个演示程序类的sereval实例。如果这不是您的情况,就不要使用它,因为您需要编写大量的代码,这通常是由mvp4g框架生成的。

此外,您也不应该直接调用bind-method。

如果需要多个特性,可以创建一个处理程序(它扩展了BaseHandler-class)。此处理程序应管理演示者实例。编码应该包含演示者实例的列表:

代码语言:javascript
复制
  private List<EventHandlerInterface<MyEventBus>> presenters = new ArrayList<EventHandlerInterface<MyEventBus>>();

若要显示新实例,甚至要将现有实例置于前面,处理程序应侦听一个事件(此处为:eventBus.showObject([someNumber]); )。

代码语言:javascript
复制
public void onShowObject(long id) {
  // check if a presenter for that Id already exists
  if (presenters.size() > 0) {
    for (int i = 0; i < presenters.size(); i++) {
      ObjectPresenter presenter = (ObjectPresenter) presenters.get(i);
      if (presenter.getId() == idNr) {
        eventBus.setCurrentObject(presenter.getView().asWidget());
        return;
      }
    }
  }
  // no presenter found, create a new one and add the presetner instance to the list 
  ObjectPresenter mainObject = eventBus.addHandler(ObjectPresenter.class);
  mainObject.setId(id);
  presenters.add(mainObject);
  eventBus.setCurrentObject(mainObject.getView().asWidget());
}

}

事件setCurrentObject(Widget widget)将使您的视图可见。因此,您的shell还必须实现为演示者/视图组合,必须侦听setCurrentObject(Widget widget)-event。使用此代码,不需要调用bind-method。

我将在几个项目中使用这段代码,它非常好用。而且,使用此代码很容易使用历史管理。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24295057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档