GWT MVP更新地点更改时的活动状态 [英] GWT MVP updating Activity state on Place change
问题描述
更新位置更改的活动状态的最佳做法是什么?想象一下,您有一个视图活动,显示类别列表和类别中的项目列表。如果选择了不同的类别,则应用程序会转到带有类别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作为参数传递)。 屏幕是一个单身人士,然后确保只加载一次类别列表。
†。我不喜欢将我的活动与他们使用的地方联系起来(我不太喜欢 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
PlaceChangeEvent
s from within the activity (you can then use aFilteredActivityMapper
andCachingActivityMapper
for the caching of the activity in yourActivityMapper
, so that it's reduced to only create a new activity when asked). †have some component listen to
PlaceChangeEvent
s and translate them to business-oriented events, the activity then listens to those events rather thanPlaceChangeEvent
s, 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'sstart
(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屋!