簡體   English   中英

將數據綁定到GWT中的UI

[英]Binding data to the UI in GWT

在Silverlight中,常用的模式是:

  1. 索取資料
  2. 取回一個空的數據容器
  3. 異步觸發查詢以填充容器
  4. 查詢返回時,在容器上觸發一個事件
  5. 根據容器的內容更新用戶界面

可以在GWT中完成嗎?

我問的原因是我正在嘗試創建一個包含組名和圖標列表的SuggestBox 首先,我Facebook查詢來獲取接近在當前字符串組ID列表SuggestBox 然后,我啟動查詢以獲取每個組ID的圖標。 問題是在完成這些查詢之前,我必須返回建議。 我不確定在獲得數據后如何返回並插入數據。 我不想在調用完成之前阻塞,也沒有真正的方法預先知道要加載哪些數據。

我可以為加載圖像的建議返回一個小部件,但是建議必須為純字符串。

這里正確的方法是什么?

假設您正在使用GWT RPC。 您將具有一些服務界面,可讓您獲取groupIds以獲得建議以及圖標獲取特定的組ID。

public interface FacebookService extends RemoteService {

    List<String> getFacebookGroupIds(String suggestion);

    Icon getIconForGroup(String groupId);
}

您應該構建自己的“建議”實施,該建議可以僅使用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();
    }
}

我使用Icon作為您自己的圖標實現,這不是標准類。 然后,您可以使ModifyOracle的實現異步獲取組id和圖標。 RecommendationOracle使用回調來通知notifyBox可以使用對請求的某些響應。 因此,獲取您的結果,並在獲取結果時調用回調。 它看起來像這樣。

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