Note that there are some explanatory texts on larger screens.

plurals
  1. POOpen social viewer state (isOwner)
    text
    copied!<p>We are creating a gadget for the opensocial API 0.7. In some functions we have to decide, if the viewer is the owner.</p> <p>We couldn't use the usual function for this purpose:<br> <code>return gadgets.util.getUrlParameters().viewer == gadgets.util.getUrlParameters().owner;</code> so we had to create a workaround and get the information via a DataRequest.</p> <p>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.</p> <p>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.</p> <p>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.</p> <pre><code>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 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload