3

我是 Google Drive SDK 的新手。我跟着

https://developers.google.com/drive/v2/reference/permissions/insert?hl=en

这个例子展示了如何更改文件的权限。我可以运行这段代码,但我的问题是我想更改多个文件的权限。我为用户尝试了一些循环,并在其中 fileId 循环,但是因为 javascript 不等待子函数完成(异步调用函数),所以它不会为我工作

请帮我

以下是我的代码...我对此感到非常困惑,请帮我解决这个问题

这是代码请在这里更正我

      var scopes = 'https://www.googleapis.com/auth/drive';

      // Use a button to handle authentication the first time.
      function handleClientLoad() {
        gapi.client.setApiKey(apiKey);
        //window.setTimeout(checkAuth,1);
        checkAuth();
      }

      function checkAuth() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
      }

      function handleAuthResult(authResult) {
        var authorizeButton = document.getElementById('authorize-button');
        if (authResult && !authResult.error) {
          authorizeButton.style.visibility = 'hidden';
          makeApiCall();
        } else {
          authorizeButton.style.visibility = '';
          authorizeButton.onclick = handleAuthClick;
        }
      }

      function handleAuthClick(event) {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        return false;
      }

      // Load the API and make an API call.  Display the results on the screen.
      function makeApiCall() {
        var fileID = new Array();
        var email = new Array();
        fileID[0] = "1fEYDfB9owJAdxQ7lI0";
        fileID[1] = "1YYcKn1ZsiPYWA";

        email[0] = "email1@email.com";
        email[1] = "email2@email.com";
        email[2] = "email3@email.com";

          gapi.client.load('drive', 'v2', function() {
              for(var i=0;i<email.length;i++)
              {
                  var body = {
                            'value': email[i],
                            'type': "user",
                            'role': "reader"
                          };
                  for(var j=0;j<fileID.length;j++)
                    {

                        var fileid = fileID[j];

                       excuteRequest(body, fileid, function() { 
                              var request = gapi.client.drive.permissions.insert({
                                    'fileId': fileid,
                                    'resource': body
                                  });
                            request.execute(function(resp) { });
                        }); 
                    }
              }
          });
    }

      function excuteRequest(param1, param2, callback) {

            if (callback && typeof(callback) === "function") {
                callback();
            }
        }
4

1 回答 1

3

使用流控制库,类似于asynchttps ://github.com/caolan/async

var files = ["id1", "id2", "id3"];
async.each(files, function(fileId, callback){

  gapi.client.drive.permissions.insert({
     'fileId': fileId,
     'resource': body
  }).execute(function(response) {
      callback(null, response);
  });

}, function(err){
  if (!err) { console.log('done.'); }
});
于 2013-04-19T11:49:46.983 回答