2 Replies Latest reply on Oct 17, 2012 12:34 PM by mark.weitzel

    Recursive calls and JavaScript API 3.0

    sven.vetter

      Hello

       

      I try to write a small app.

      Goals:

      Display all space and some action per space (change notification and so on).

       

      But I have problems with the recursive calls. I start by the root space and try to find all child and for all child the same ...

       

      Code:

      gadgets.util.registerOnLoadHandler(function() {
        // add code that should run on page load here
        
        var vTable = $('<table/>');
        var request=osapi.jive.corev3.places.get({"uri":"/places/1001"});
        vTable.append( '<tr><th>Space</th></tr>' );
        
        
        request.execute(function (response) {
              //console.log("Fetched :",response);
              DisplaySpace(response, vTable, 1);
              
              $('#here_table').append(vTable);
      
        });
        // resize app window to fit content
         gadgets.window.adjustHeight();
      
      });
      
      //----------------------------------------------------------------------------------------
      function DisplaySpace (pSpace, vTable, pLevel) {
        console.log("in DisplaySpace with " + pSpace.name + " Level=" + pLevel + " Child count=" + pSpace.childCount);
        vTable.append( '<tr><td>' +pLevel +'</td><td>' +pSpace.name +'</td></tr>' );
        
        if (!pSpace.childCount) return;
        pLevel++;
        var request=pSpace.getPlaces();
        request.execute(function (response) {
      //    console.log("Childs: ",response)
        var vChilds=response.list;
        for (i = 0; i < vChilds.length; i++) {
          var vChild = vChilds[i];
          console.log("- Call DisplaySpace with " + vChild.name + " from " + pSpace.name);
          DisplaySpace(vChild, vTable, pLevel);
        }
        });
      }
      

       

      But it doesn't work correctly. Some spaces (on level 2) are called twice.

       

      This is my space overview (on my small test instance):

      Screen Shot 2012-10-16 at 19.26.42.png

      console.log results:

      Screen Shot 2012-10-16 at 19.29.43.png

       

      Any help?

        • Re: Recursive calls and JavaScript API 3.0

          Sven,

           

          Try this code:

           

           

           

           

          function handleResponse(data) {

              output("in handle response");

              console.log(data);

              output(gadgets.json.stringify(data));

           

           

           

           

              if (data.hasOwnProperty("list")) {

                  console.log(data); // We've got a collection of places

                  for (var i = 0; i < data.list.length; i++) {

                      var aPlace = data.list[i];

                      aPlace.getPlaces().execute(handleResponse);

                  }

           

           

              }

              else {

                  data.getPlaces().execute(handleResponse);

              };

          };

           

           

          function request() {

              var params = {

                  type : "space",

                  success : ( function(data){debugger; osapi.jive.corev3.places.get({"uri":data.resources.self.ref}).execute(handleResponse);} ),

                  error : handleResponse  };

              osapi.jive.corev3.places.requestPicker(params);

           

           

          };

           

           

           

           

          request();

           

          When I run this, I get the top level space (b/c handleResponse is called) and then only the sub-space.