简体   繁体   English

将数据绑定到GWT中的UI

[英]Binding data to the UI in GWT

In Silverlight, a frequently used pattern is: 在Silverlight中,常用的模式是:

  1. Request data 索取资料
  2. Get back an empty container for the data 取回一个空的数据容器
  3. Asynchronously fire off a query to fill the container 异步触发查询以填充容器
  4. When the query returns, fire an event on the container 查询返回时,在容器上触发一个事件
  5. Update the UI according to the container's contents 根据容器的内容更新用户界面

Can this be done in GWT? 可以在GWT中完成吗?

The reason I ask is that I'm trying to make a SuggestBox that contains a list of group names and icons. 我问的原因是我正在尝试创建一个包含组名和图标列表的SuggestBox First, I query Facebook to get a list of groups IDs that are close to the current String in the SuggestBox . 首先,我Facebook查询来获取接近在当前字符串组ID列表SuggestBox Then, I fire off queries to get icons for each group id. 然后,我启动查询以获取每个组ID的图标。 The problem is that I have to return the suggestions before those queries are done. 问题是在完成这些查询之前,我必须返回建议。 I'm not sure how to go back and insert the data after I have it. 我不确定在获得数据后如何返回并插入数据。 I don't want to block until the calls are complete, and there's no real way to know in advance what data to load. 我不想在调用完成之前阻塞,也没有真正的方法预先知道要加载哪些数据。

I could return a widget for the suggestion that loads an image, but the suggestion must be a plain String. 我可以为加载图像的建议返回一个小部件,但是建议必须为纯字符串。

What is the right approach here? 这里正确的方法是什么?

Let's assume you're using GWT RPC. 假设您正在使用GWT RPC。 You'll have some service interface that lets you fetch the groupIds for a suggestion and the icon for a specific group id. 您将具有一些服务界面,可让您获取groupIds以获得建议以及图标获取特定的组ID。

public interface FacebookService extends RemoteService {

    List<String> getFacebookGroupIds(String suggestion);

    Icon getIconForGroup(String groupId);
}

You should build your own implementation of Suggestion that can display itself with either just a groupId or a groupId and an Icon. 您应该构建自己的“建议”实施,该建议可以仅使用groupId或groupId和一个Icon进行显示。

public class FacebookGroupSuggestion implements Suggestion {

    private String groupId;

    private Icon icon;

    public FacebookGroupSuggestion(String groupId) {
        this.groupId = groupId;
    }

    public String getDisplayString() {
        StringBuilder builder = new StringBuilder();
        builder.append("<b>");
        builder.append(this.groupId);
        builder.append("</b>");
        if (this.icon != null) {
            builder.append(this.icon.toSafeHtml());
        }
        return builder.toString();
    }
}

I'm using Icon as your own implementation of an icon, it's not a standard class. 我使用Icon作为您自己的图标实现,这不是标准类。 Then, you can make your implementation of SuggestOracle to fetch the groupIds and icons asynchronously. 然后,您可以使ModifyOracle的实现异步获取组id和图标。 The SuggestOracle uses a callback to inform the suggestBox that some response to a request is available. RecommendationOracle使用回调来通知notifyBox可以使用对请求的某些响应。 So fetch your results, and call the callback when you get them. 因此,获取您的结果,并在获取结果时调用回调。 It'll look something like this. 它看起来像这样。

public class FacebookSuggestOracle extends SuggestOracle {

    private FacebookServiceAsync service = GWT.create(FacebookService.class);
    private Request currentRequest;
    private Callback currentCallback;

    @Override
    public void requestSuggestions(Request request, Callback callback) {
        // Save request & callback for future use.
        this.currentRequest = request;
        this.currentCallback = callback;

        // Fetch the groupIds
        service.getFacebookGroupIds(request.getQuery(), new AsyncCallback<List<String>>() {
            public void onSuccess(List<String> result) {
                createSuggestionsForGroupIds(result);
            }

        });

    }

    private void createSuggestionsForGroupIds(List<String> groupIds) {
        List<FacebookGroupSuggestion> suggestions = new ArrayList<FacebookGroupSuggestion>();
        for (String groupId : groupIds) {
            suggestions.add(new FacebookGroupSuggestion(groupId));
        }
        Response response = new Response(suggestions);
        // Tell the suggestBox to display some new suggestions
        currentCallback.onSuggestionsReady(currentRequest, response);

        // Fetch the icons
        for (String groupId : groupIds) {
            service.getIconForGroup(groupId, new AsyncCallback<Icon>() {

                public void onSuccess(Icon result) {
                    // match the icon to the groupId in the suggestion list
                    // use the callback again to tell the display to update itself

                }

            });
        }
    }
}

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

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