views:

30

answers:

1

We are creating a gadget for the opensocial API 0.7. In some functions we have to decide, if the viewer is the owner.

We couldn't use the usual function for this purpose:
return gadgets.util.getUrlParameters().viewer == gadgets.util.getUrlParameters().owner; so we had to create a workaround and get the information via a DataRequest.

The DataRequest calls a callback function and has no useable return value. We tried a quick hack by using global variables to set the corresponding value.

The issue at this point is, that the function does not 'wait' for the callback-function to be finished. We know this is no good code/style at all, but we tried to force a timeout for debug reasons.

Handling all the code within the callback-function (as suggested in the examples of the opensocial docs) is not possible. We are looking for something like a real 'sleep()' in JavaScript to wait for the callback-function to complete or another alternative to get the owner information about the viewer.

globalWorkaroundIsOwner = false;  

function show_teaser(){  
  if (current_user_is_owner()){  
    // ...  
  }  
  // ...  
}  

function current_user_is_owner() {  

  var req = opensocial.newDataRequest();  
  req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER), 'viewer');  

  // This will set the the correct value  
  req.send( user_is_owner_workaround );  

  // This is an attempt to delay the return of the value.  
  // An alert() at this point delays the return as wanted.  
  window.setTimeout("empty()", 2000);  

  // This return seems to be called too early (the variable is false)  
  return globalWorkaroundIsOwner;  
}  

function user_is_owner_workaround(dataResponse) {  
  var viewer = dataResponse.get('viewer').getData();  

  globalWorkaroundIsOwner = viewer.isOwner();  
  // value is correct at this point  
}
+1  A: 

Can you use an additional flag in order to indicate whether the remote query has already returned the required value?

var globalWorkaroundIsOwner = false;
var workaroundStarted = false, workAroundComplete = false;
var checker;

function show_teaser(){
    if (!workaroundStarted) {
        workaroundStarted = true;
        current_user_is_owner();
    }
    if (workaroundComplete) {  
    if (globalWorkaroundIsOwner){  
        // ...  
    }  
    // ...  
      if (checker) {
      clearInterval(checker);
      }
    }
}  

function current_user_is_owner() {  

    var req = opensocial.newDataRequest();  
    req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER), 'viewer');  

    checker = setInterval("show_teaser()", 1000);
    // This will set the the correct value  
    req.send( user_is_owner_workaround );  
}  

function user_is_owner_workaround(dataResponse) {  
    var viewer = dataResponse.get('viewer').getData();  

    globalWorkaroundIsOwner = viewer.isOwner();  
    workAroundComplete = true;
    // value is correct at this point  
}
David Parunakian
We couldn't use it this way, but the basic idea of using setInterval() helped to initialize the required values at the beginning.This way we had to implement only one interval/checker.Thanks!
Smamatti