打开社交查看器状态 (isOwner)

发布于 2024-09-06 16:16:00 字数 1369 浏览 7 评论 0原文

我们正在为 opensocial API 0.7 创建一个小工具。 在某些功能中,我们必须决定观看者是否是所有者。

我们无法使用通常的函数来实现此目的:
返回 gadgets.util.getUrlParameters().viewer == gadgets.util.getUrlParameters().owner; 因此我们必须创建一个解决方法并通过 DataRequest 获取信息。

DataRequest 调用回调函数并且没有可用的返回值。 我们尝试了一种快速破解方法,使用全局变量来设置相应的值。

此时的问题是,该函数不会“等待”回调函数完成。我们知道这根本不是好的代码/风格,但我们尝试出于调试原因强制超时。

处理回调函数中的所有代码(如 opensocial 文档示例中所建议的)是不可能的。 我们正在 JavaScript 中寻找类似真正的“sleep()”的东西来等待回调函数完成,或者另一种替代方法来获取有关查看者的所有者信息。

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  
}

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  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2024-09-13 16:16:00

您可以使用附加标志来指示远程查询是否已返回所需的值吗?

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  
}

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  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文