GWT MVP更新地点更改时的活动状态 [英] GWT MVP updating Activity state on Place change

查看:103
本文介绍了GWT MVP更新地点更改时的活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新位置更改的活动状态的最佳做法是什么?想象一下,您有一个视图活动,显示类别列表和类别中的项目列表。如果选择了不同的类别,则应用程序会转到带有类别ID的新位置。然后,我想只刷新项目,而不是创建新的活动,也重新读取类别列表。



我目前的方法是这样的:

  public class AppActivityMapper implements ActivityMapper {

private ItemListActivity itemListActivity;

...

公共活动getActivity(最终地方地点){
final活动活动;
$ b $ if(place instanceof ItemListPlace){
if(itemListActivity == null){
itemListActivity = new ItemListActivity((ItemListPlace)place,clientFactory);
} else {
itemListActivity.refresh((ItemListPlace)place);
}
activity = itemListActivity;
} else {
itemListActivity = null;
}

...
返回活动;
}

...


解决方案 从活动中(您可以使用 FilteredActivityMapper CachingActivityMapper 来进行缓存您的 ActivityMapper 中的活动,因此它被减少为仅在被询问时创建新活动)。 †


  • 有一些组件监听 PlaceChangeEvent s和翻译它们到面向业务的事件,则活动会监听那些事件,而不是 PlaceChangeEvent s,否则与上面相同。


  • 将活动与屏幕分离,使用 reset()方法将屏幕设置为一个单例,并从活动的 start (在这种情况下可能将类别ID作为参数传递)。 屏幕是一个单身人士,然后确保只加载一次类别列表。

  • 在您的情况下,您也可以简单地将类别列表一个共享的缓存,这样你就不需要重复使用你的活动了,可以创建一个新的,分类列表将被检索一次并放入缓存中,随后的活动实例将只使用缓存中的内容。这与上述类似,但更简单,并且缓存可被应用程序的其他部分使用。 虽然我个人宁愿采用你的方法(有一个小例外,见下文),因为它是最简单/最简单的方法。将活动与屏幕分离也是一种选择; GWT团队开始在Expenses样本中探索这种方法(将使用MVP的活动责任与演示者责任分离开来),而不幸的是完成了它。除此之外,我不要认为现在真的有什么最佳做法出现了。






    †。我不喜欢将我的活动与他们使用的地方联系起来(我不太喜欢 goTo 调用的耦合,但尚未找到一个干净而简单的选择),所以我宁愿不选择这个选项;同样,我也不会像你那样把这个地方传递给活动构造函数和 refresh 方法,而是将信息提取出来并传递给活动(例如在你的情况下,只给活动分类ID,而不是 ItemListPlace 实例;然后我会简单地调用 setCategory 在所有情况下,甚至没有将类别ID传递给构造函数)。


    What is the best practise to update Activity state on Place change? Imagine you have an activity with view that displays list of categories and list of items in the category. If different category is selected then app goes to new place with category ID. I want then to only refresh items and not to create new activity that also re-reads category list.

    My current approach is like this:

    public class AppActivityMapper implements ActivityMapper {
    
        private ItemListActivity itemListActivity;
    
        ...
    
        public Activity getActivity(final Place place) {
            final Activity activity;
    
            if (place instanceof ItemListPlace) {
                if (itemListActivity == null) {
                    itemListActivity = new ItemListActivity((ItemListPlace) place, clientFactory);
                } else {
                    itemListActivity.refresh((ItemListPlace) place);
                }
                activity = itemListActivity;
            } else {
                itemListActivity = null;
            }
    
            ...
            return activity;
        }
    
        ...
    

    解决方案

    Alternatives are:

    • listen to PlaceChangeEvents from within the activity (you can then use a FilteredActivityMapper and CachingActivityMapper for the caching of the activity in your ActivityMapper, so that it's reduced to only create a new activity when asked). †

    • have some component listen to PlaceChangeEvents and translate them to business-oriented events, the activity then listens to those events rather than PlaceChangeEvents, otherwise the same as above.

    • decouple the activity from the "screen", make the "screen" a singleton with a reset() method and call that method from the activity's start (possibly passing the category ID as an argument in this case). The "screen" being a singleton could then make sure to load the categories list only once.

    • in your case, you could also simply put the categories list in a shared cache, so that you don't have to reuse your activity by can create a new one, the categories list will be retrieved once and put in the cache, subsequent activity instances will just use what's in the cache. This is similar to the above, but simpler, and the cache could be used by other parts of the application.

    I'd personally rather go with your approach though (with a small exception, see below), as it's the simplest/easiest. Decoupling the activity from the "screen" is also an option; the GWT Team started exploring this approach in the Expenses sample (decoupling the activity responsibility from the presenter responsibility with using MVP) without ever finishing it unfortunately.

    Other than that, I don't think any best practice has really emerged for now.


    †. I don't like coupling my activities with the places they're used with (I don't quite like the coupling for the goTo calls either, but haven't yet found a clean and simple alternative), so I'd rather not go with this option; and similarly, I'd not pass the place to the activity constructor and refresh method like you did, but rather extract the information out of the place and pass it to the activity (e.g. in your case, only give the category ID to the activity, not the ItemListPlace instance; I would then simply call setCategory in all cases, and not even pass the category ID to the constructor).

    这篇关于GWT MVP更新地点更改时的活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆