在活动中使用 GWT 的 RequestFactory

发布于 2024-11-15 04:59:33 字数 1189 浏览 4 评论 0原文

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

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

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

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

  • 触发 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方法已经返回了最新的Customer,因此我不需要在处理EntityProxyChange期间再次获取客户;如果我可以避免的话,我不想承担再次调用服务器的费用。

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

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

My CustomerActivity class happens also to be a presenter in the MVP sense. In response to actions by the user, the following code is called:

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

When the above code executes, two things happen:

  • I receive an updated copy of the Customer, with which I can refresh the state of the view

  • An EntityProxyChange event is fired

The CustomerActivity listens for EntityProxyChange events because other Activities also make changes to customer records, and I want to keep the CustomerActivity up-to-date.

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?
    }
  });

Since theupdate method already returns an up-to-date Customer, I don't need to fetch the customer again during processing of the EntityProxyChange; and I don't want to incur the cost of another call to the server if I can avoid it.

I was hoping that the EntityProxyChange class would provide me with the entity's version number, which I could compare with the version of the customer I have cached. No dice.

I suppose I can set some kind of inTheMiddleOfAnUpdateOperation flag, and check it before fetching the customer. Is that what people are doing? The idea makes me gag just a little bit. Can you suggest a better way to write an activity that listens for changes and makes changes to the same entity types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山有枢 2024-11-22 04:59:33

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

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 等..)

I'm not sure this would work, but you could try to create another event bus :

final EventBus customerActivityEventBus = new SimpleEventBus();

Initialize a new RequestFactory with this eventBus and use it in the CustomerActivity.

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

Your CustomerActivity will still listen to change events from the main eventBus but will not see the events it fired.
In your other activities, listen to events either only from the customerActivityEventBus or from both, depending on what you want.

Of course, keep only one main eventBus to use for events that are not from a Request Factory (ie. ActivityManager, PlaceHistoryHandler, etc ..)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文