Cascading call into a REST Web Service - best way to approach it

Cascading call into a REST Web Service - best way to approach it

I am using getJSON to retrieve data from a REST web service. Works fine with one call, but I'm confounded on my approach to obtaining data when I need to hit another REST web service while cycling through the first call. To pseudoecode this:

  1. Call REST Web Service
  2. Cycle through JSON return to build output
  3. When get to the state code
    1. Call Rest Web Service for States with the State ID from 3
    2. Return the State Info
  4. Output State Info
The code I have so far which breaks down at the GetStateInfo because of some scoping issues with stateDisplayName is here. I don't think its the best approach to what I am calling a cascading REST call, but I don't seem to be able to come up with something else.

  function GetNonNull(value) {
            return (value == null) ? 'empty' : value;
        }

        function GetStateInfo(stateId) {
            var endpoint = "/_vti_bin/ListData.svc/StateList(" + stateId + ")";
            var stateDisplayName = "";
            $(".pmgrState").ready(function () {
                $.getJSON(endpoint, function (data) {
                    var stateName = data.d.StateName;
                    var stateAbbr = data.d.StateAbbreviation;
                    stateDisplayName = stateName + "(" + stateAbbr + ")";

                    alert(stateDisplayName);
                });
            });

            alert(stateDisplayName);

            return $(stateDisplayName);
        }

        $(document).ready(function () {
            $.getJSON("/_vti_bin/ListData.svc/PartnerList", function (data) {
                var count = 0;
                var startHtml = "<table border='1' style='float: left' border='1'>";
                var endHtml = "</table>"
                var html = "";

                $.each(data.d.results, function (i, result) {
                    var src = result.__metadata.uri;
                    var stateId = result.StateId;
                    var stateUrl = result.State.__deferred.uri;

                    html = html + "<tr><td style='color:blue'>" + GetNonNull(result.PartnerName) +
                                    "</td><td>" + GetNonNull(result.City) +
                                    "</td><td>" + GetNonNull(src) +
                                    "</td><td>" + GetNonNull(stateId) +
                                    "</td><td>" + GetNonNull(stateUrl);

                    if (stateId != null) {
                        // var stateName = GetStateName(stateId);
                        var stateName = GetStateInfo(stateId);
                        html = html + "</td><td>";
                    } else {
                        html = html + "</td><td>";
                    }

                    html = html + "</td></tr>";
                });


                $('#resultarea').append($(startHtml + html + endHtml));

            });

        });






























































    • Topic Participants

    • cobb