2015-01-09 98 views
0

在我的聚合物頁面上,我嘗試登錄到我的google plus頁面並檢索我的名字和封面照片網址。 爲此我使用運行下面的功能,當登錄完成一個google-signin元素:GooglePlus登錄處理結果

loginSuccess: function(e){ 
    console.log("Success!"); 
    gapi.client.load('plus', 'v1', function() { 
     var request = gapi.client.plus.people.get({'userId' : 'me'}); 
     request.execute(function(retr){ 
      console.log("Data Loaded"); 
      console.log(retr); 
      this.myFunction(retr); 
     }); 
    }); 
} 

可以到「數據加載」一切都很正常,也控制檯打印出從谷歌以及整個結果對象。但我無法執行功能(this.myFunction)或從那裏訪問聚合物數據。我如何將來自gapi-request的結果數據存儲到我的聚合物變量中?

感謝,馬爾特

回答

0

我才發現,我選擇了錯誤的方式來獲得我的個人資料數據。

有一種聚合物元素可以從google-api獲取數據,稱爲google-api-loaderhttp://googlewebcomponents.github.io/google-apis/components/google-apis/#google-api-loader)。我使用的元素通過以下方式附加於google-signin元素:

<google-api-loader id="plus" name="plus" version="v1" on-google-api-load="{{displayProfile}}"> </google-api-loader>

這裏的功能displayProfile

displayProfile: function(){ 
     if (this.signedIn && this.$.plus.api) { 
      var request = this.$.plus.api.people.get({"userId": "me"}); 
      request.execute(function (resp) { 
       this.userData = resp; 
      }.bind(this)); 
     } 
    } 

它工作真棒,我通過this.userData獲得對數據的訪問。這裏還有另一個例子:https://github.com/Scarygami/google-signin-samples/blob/master/elements/google-signin-status.html

0

裏面的阿賈克斯結果,this背景不同。你需要設置一個變量你的Ajax請求之前,或者你的函數之前訪問它,就像這樣:

self: this, 

loginSuccess: function(e){ 
    console.log("Success!"); 
    gapi.client.load('plus', 'v1', function() { 
     var request = gapi.client.plus.people.get({'userId' : 'me'}); 
     request.execute(function(retr){ 
      console.log("Data Loaded"); 
      console.log(retr); 
      self.myFunction(retr); 
     }); 
    }); 
} 
+0

謝謝,但它不起作用。控制檯說:'成功! 加載數據 未捕獲TypeError:未定義不是函數「...任何想法? – Jokus 2015-01-11 14:29:13

+0

你能發表更多你的代碼嗎?就像包含displayProfile函數的整個對象一樣。 – 2015-01-11 18:02:39