2017-04-19 109 views
1

我使用Skygear作爲BaaS。 我創建了一個聚合物元素skygear-app(就像polymerfire一樣)來初始化天空實例。數據綁定與聚合物中的兒童子元素

<skygear-app name="skygear" app={{skygear}} end- 
    point="https://xyz.skygeario.com/" api-key="SECRET"></skygear-app> 

我也可以做成功的登錄由

passwordSignIn: function(){ 
    skygear.loginWithEmail(this.email, this.password).then(
     function(){ 
     console.log('login ok'); 
     console.log(skygear.currentUser.id); 
     this.currentUser = skygear.currentUser; 
     } 
    ); 

我可以在userId通過console.log(skygear.currentUser.id);得到,但是, 我不能使用獲取用戶標識數據綁定{{skygear.currentUser.id}}

<h4>Access Token: [[skygear.accessToken]]</h4> <!-- working --> 
    <h4>User Id: [[skygear.currentUser.id]]</h4> <!-- not working --> 

1級屬性像{{skygear.endpoint}}工程; 但是像{{skygear.currentUser.id}}那樣的第2級屬性沒有。

數據綁定僅限於1級嗎?和任何想法解決?

回答

1

按照polymer1.0 DOC,2級屬性是Unobservable changes,參:https://www.polymer-project.org/1.0/docs/devguide/data-system#unobservable-changes

我們可以使用由skygear容器提供onUserChanged通知聚合物應用程序。

這裏是最小工作聚合物skygear-應用

Polymer({ 
    is: 'skygear-app', 
     apiKey: { 
     type: String 
     }, 
     endPoint: { 
     type: String 
     }, 
     app: { 
     type: Object, 
     notify: true, 
     computed: '__computeApp(name, apiKey, endPoint)' 
     } 
    }, 
    __computeApp: function(name, apiKey, endPoint) { 
     if (apiKey && endPoint) { 
     skygear.onUserChanged(() => { 
      this.notifyPath('app.currentUser.id'); 
     }); 
     skygear.config({ 
      apiKey: apiKey, 
      endPoint: endPoint 
     }).then(() => { 
      this.notifyPath('app.currentUser.id'); 
     }); 
     return skygear 
     } else { 
     return null; 
     } 

     return skygear 
    } 
    }); 
+0

了'this'裏面匿名函數'。那麼()'不是'正確this'。 XD –

+0

配置後我們不必通知。他們應該是獨立的。 –

+1

我認爲應該有2個notifyPath。 首先是onUserChanged(),它在用戶登錄,註銷或更改電子郵件時更新值。 其次是在config()/ init(),它獲取當前登錄的用戶。 (雖然'this'的範圍不是代碼中的預期範圍) –