Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer, how to get url params in page.js into polymer element?

I want to make a single page application. When I go to path like localhost/person-info/101, how can I get the URL params using page.js into a Polymer element?

I have to use these params to select or change data in the person-info element before using neon-animated-page to change page

Contents of app.js:

window.addEventListener('WebComponentsReady', function () {
    var app = document.querySelector('#app');
    page('/', home);
    page('/person-info/:user', showPersonInfo);

    page({
        hashbang: true
    });

    function home() {
        app.route = 'index';

        console.log("app route" + app.route);
        console.log("home");
    }

    function showPersonInfo(data) {
        console.log(data);
        console.log(data.params.user);

        app.params = data.params;

        app.route = 'person-info';
    }
});

And my Polymer element person-info.html

<link rel="import" href="bower_components/iron-list/iron-list.html">
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/paper-toolbar/paper-toolbar.html">
<link rel="import" href="bower_components/paper-icon-button/paper-icon-button.html">

<script type="text/javascript" src="bower_components/jquery-2.1.4.min/index.js"></script>
<script src="js/app.js"></script>

<dom-module id="person-info">
    <style>
        :host {
            display: block;
            font-family: sans-serif;
            @apply(--layout-fit);
            @apply(--layout-vertical);
        }

        .toolbar {
            background: #3f78e3;   
        }

        a {
            color:#FFF;
        }
    </style>
    <template id="person-info">
        <paper-toolbar class="toolbar">
            <a href="/">
                <paper-icon-button icon="icons:arrow-back"></paper-icon-button>
            </a>

            <div>test</div>
            <div>test</div>
        </paper-toolbar>
        <content></content>
    </template>
    <script>
        Polymer({
            is: 'person-info',
            ready: function () {
                console.log("person-info page");
                this.testdd = "adisak";
                console.log("user ---->"+this.params);
                console.log(this);
            },
            properties: {
                dataindex: String
            }
        });
    </script>
</dom-module>
like image 657
Adisak Chaiyakul Avatar asked Oct 31 '22 23:10

Adisak Chaiyakul


1 Answers

Variable app is global, so if you bind a data to app.params from your route than you can access it everywhere using app.params. Or just access your element and set the properties on your element like below

function showPersonInfo(data){
    var personInfo=document.querySelector('person-info');
    personInfo.user=data.params;
    app.route = 'person-info';
}

From your person-info element you can access by using this.user but i think you need to set the property user on element properties.

like image 92
tyohan Avatar answered Nov 08 '22 11:11

tyohan