signedIn property of domHost is false on ready function call

你离开我真会死。 提交于 2019-12-25 07:32:38

问题


I want to check if the user is logged in on element creation and eventually redirect him if the user is not. The problem is that the domHost.signedIn property is false even though the user is signedIn. If I check the property later(for example when I call a function with button tap) the property is true as it should be.

Here is the code:

<link rel="import" href="../bower_components/polymer/polymer.html">

<link rel="import" href="/bower_components/paper-button/paper-button.html">

<dom-module id="settings-view">
    <template>
        <style>
        </style>
        TODO: user settings
        <paper-button on-tap="debugFunction">button</paper-button>
    </template>

    <script>
        Polymer({
            is: 'settings-view',

            ready: function () {
                console.log(this.domHost.signedIn); // false
                console.log(this.domHost.user);  // null
            },

            debugFunction: function () {
                console.log(this.domHost.signedIn); // true
                console.log(this.domHost.user); // user object
            }
        });
    </script>
</dom-module>

What is the best way to check if the user is signedIn in child element? Would setting the signedIn value to iron-meta element be a better approach?

Thanks, Jan


回答1:


You're better off declaring properties with observers on them. Observers will execute the function as soon as the property's value is something other than undefined. So your code will look like this:

<link rel="import" href="../bower_components/polymer/polymer.html">

<link rel="import" href="/bower_components/paper-button/paper-button.html">

<dom-module id="settings-view">
    <template>
        <style>
        </style>
        TODO: user settings
    </template>

    <script>
        Polymer({
            is: 'settings-view',

            properties: {
                signedIn: {
                    type: Boolean,
                    observer: '_signedInChanged'
                },

                user: {
                    type: Object,
                    observer: '_userChanged'
                },
            },

            _signedInChanged: function (newSignedInValue) {
                console.log(newSignedInValue); // true
                console.log(this.signedIn); // true
            },

            _userChanged: function (newUserValue) {
                console.log(newUserValue); // user object
                console.log(this.user); // user object
            }
        });
    </script>
</dom-module>

Then when you update those signedIn and user values through JavaScript or data binding, the observers will call the associated functions.



来源:https://stackoverflow.com/questions/40729512/signedin-property-of-domhost-is-false-on-ready-function-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!