1

我的CustomerActivity班级恰好也是 MVP 意义上的演讲者。为了响应用户的操作,调用以下代码:

context.update(customer).fire(new Receiver<CustomerProxy>() {
  public void onSuccess(CustomerProxy response) {
    // set the view according to the response
  }
});

当上面的代码执行时,会发生两件事:

  • 我收到了客户的更新副本,我可以用它刷新视图的状态

  • 触发 EntityProxyChange 事件

CustomerActivity监听 EntityProxyChange 事件,因为其他活动也会更改客户记录,我想保持CustomerActivity最新。

EntityProxyChange.registerForProxyType(eventBus, CustomerProxy.class,
  new EntityProxyChange.Handler<CustomerProxy>() {
    public void onProxyChange(EntityProxyChange<CustomerProxy> event) {
      fetchCustomer(event.getProxyId());
      // ...but what if it was me that made the change?
    }
  });

由于该update方法已经返回了一个最新的客户,我不需要在处理EntityProxyChange;期间再次获取客户。如果可以避免的话,我不想承担再次调用服务器的费用。

我希望EntityProxyChange该类能够为我提供实体的版本号,我可以将其与我缓存的客户版本进行比较。没有骰子。

我想我可以设置某种inTheMiddleOfAnUpdateOperation标志,并在获取客户之前检查它。人们正在这样做吗?这个想法让我有点作呕。您能否建议一种更好的方法来编写一个监听更改并对相同实体类型进行更改的活动?

4

1 回答 1

1

我不确定这是否可行,但您可以尝试创建另一个事件总线:

final EventBus customerActivityEventBus = new SimpleEventBus();

用这个 eventBus 初始化一个新的 RequestFactory 并在 CustomerActivity 中使用它。

customerActivityRequestFactory = GWT.create(CustomerRequestFactory.class);
customerActivityRequestFactory.initialize(customeActivityEventBus);
CustomerRequest context = customerActivityRequestFactory.customerRequest();

您的 CustomerActivity 仍将监听来自主 eventBus 的更改事件,但不会看到它触发的事件。在您的其他活动中,根据您的需要,仅收听来自 customerActivityEventBus 或两者的事件。

当然,只保留一个主 eventBus 用于不是来自请求工厂的事件(即 ActivityManager、PlaceHistoryHandler 等..)

于 2011-06-10T10:26:00.450 回答