【发布时间】:2016-10-04 15:41:53
【问题描述】:
我问这个问题,因为这些问题无法正确回答:
In Polymer 1.0 how can I databind to a boolean property of an element?
Polymer 1.x: How to data bind to a variable boolean attribute?
How to bind paper-toggle-button to a Boolean in Polymer
我有这个元素。简而言之,它的作用是在用户登录时反映“登录”属性,如果用户未通过身份验证,则不设置它。
<dom-module id="my-oauth">
<template>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'my-oauth',
ready : function() {
this.verifyLogin();
},
properties : {
loggedIn : {
type : Boolean,
reflectToAttribute: true,
value : false
}
},
observers : ['verifyLogin()'],
verifyLogin: function () {
var val = localStorage.getItem("token") === null
|| localStorage.getItem('token_expiration_time') == null
|| localStorage.getItem('token_type') == null
|| localStorage.getItem('token_expiration_created_date') == null
|| !isTokenValid(localStorage.getItem('token_expiration_time'),
localStorage.getItem('token_expiration_created_date'))
? false : true;
if (val) {
this.setAttribute('logged-in',true);
} else {
this.removeAttribute('logged-in');
}
},
logout : function() {
localStorage.removeItem("token");
console.log("Logged out!");
this.verifyLogin();
},
storeLocal(token, expiration, tokenType, issued) {
issued = typeof issued !== 'undefined' ? issued : Date.now();
localStorage.setItem("token", token);
localStorage.setItem("token_expiration_time", expiration);
localStorage.setItem("token_type", tokenType);
//The Time in which was set.
localStorage.setItem("token_expiration_created_date", issued);
this.verifyLogin();
}
});
})();
function receiveToken(token) {
console.log("Token Received.");
console.log(token);
}
</script>
</dom-module>
问题是,如何读取“登录”属性?
例如:
<my-oauth logged-in$="[[authenticated]]"></my-oauth>
<template is="dom-if" if="[[!authenticated]]">
<h1>User is not logged in</h1>
</template>
<template is="dom-if" if="[[authenticated]]">
<h1>User has logged in</h1>
</template>
我也从这两个问题中尝试过:
<my-oauth id="js-ouath" {{loggedIn}}></my-oauth>
<template is="dom-if" if="{{!loggedIn}}">
<h1>User is not logged in</h1>
</template>
<template is="dom-if" if="{{loggedIn}}">
<h1>User has logged in</h1>
</template>
这些都不起作用。通知程序工作正常。我错过了什么?
【问题讨论】:
标签: data-binding polymer