简体   繁体   English

GWT活动:添加了小部件以使用浏览器后退按钮再次查看

[英]GWT Activities: Widget added to view again when using browser back button

The way I understand it, if I want to add widgets dynamically to a view created with UIBinder, I would do that in the start method of the activity that is the presenter for that view. 以我的理解,如果我想动态地将小部件添加到使用UIBinder创建的视图中,则可以在该视图的演示者的活动的start方法中进行操作。

Here's my code: 这是我的代码:

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    view = clientFactory.getDashboardView();
    view.setPresenter(this);

    ArrayList<Department> deps = ModelFactory.getDepartments();
    view.passData(deps); // Correct?
    panel.setWidget(view.asWidget());

}



public void passData(ArrayList<Department> departments) {

TextCell text = new TextCell();
CellList<String> cellList = new CellList<String>(text);
String[] departmentNames = new String[departments.size()];
for (int i = 0; i < departments.size(); i++) {
    departmentNames[i] = departments.get(i).getName();
}
cellList.setRowData(Arrays.asList(departmentNames));
departmentsDiv.add(cellList);

} }

It works. 有用。 However, when using the back button to navigate to the previous place and back, the widget is added again. 但是,当使用“后退”按钮导航到上一个位置并返回时,将再次添加该小部件。

How do I handle this correctly? 如何正确处理?

You have two options: 您有两种选择:

  1. If you don't want to refresh the data on each visit to this view, you need to add a flag to the view to tell if the data has been already populated. 如果您不想在每次访问此视图时刷新数据,则需要在该视图中添加一个标志,以告知数据是否已被填充。 Then, when this view is visited again, your activity should call view.passData(deps); 然后,当再次访问该视图时,您的活动应调用view.passData(deps); only if the flag is set to false. 仅当标志设置为false时。 After the data is loaded, set the flag to true. 加载数据后,将标志设置为true。

  2. If you do want to refresh the data on each visit, call departmentsDiv.clear() before adding a new CellList. 如果您确实想在每次访问时刷新数据,请在添加新的CellList之前先调用departmentsDiv.clear()

NB: A better approach is to create your CellList once, when the view is displayed for the first time, and then only call setRowData when the new data is available. 注意:更好的方法是在第一次显示视图时一次创建CellList,然后在新数据可用时仅调用setRowData

You are creating View object by using Factory method. 您正在使用Factory方法创建View对象。 You should consider creating views during application load using, for instance: GIN and marking them as Singletons. 您应该考虑在应用程序加载期间创建视图,例如使用:GIN并将其标记为Singletons。 The proper way is to pass them as start() method parameters and just set presenter reference on them. 正确的方法是将它们作为start()方法参数传递,并仅在其上设置演示者引用。

General idea is to make Views singletons. 总体思路是使Views单身。 Activities should be created while throwing GWT place (stateless) and just using singleton Views, so you can keep your view input data. 应该在扔GWT场所(无状态)的同时仅使用Singleton Views创建活动,以便可以保留视图输入数据。

Read tutorial here on using MVP / GIN pattern: http://blog.hivedevelopment.co.uk/2009/08/google-web-toolkit-gwt-mvp-example.html 在此处阅读有关使用MVP / GIN模式的教程: http : //blog.hivedevelopment.co.uk/2009/08/google-web-toolkit-gwt-mvp-example.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM