Search code examples
polymer

how to use data from one page in polymer 2.0 in another


I have a html file which makes a rest call and gets data in an array. How can I use this another file. I want to be able to access and manipulate the data and print each element in the array on click of next button. my-assessment.html contains the data loaded and my-quiz.html should contain the next and previous button to loop through the data.

my-assessment.html

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="../bower_components/polymer-simple-slider/simple-slider.html">
<link rel="import" href="../bower_components/promise-polyfill/promise-polyfill-lite.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">



<dom-module id="my-assessment">
    <template>          
        <button on-click="getAssessment">Load Assessment</button>

        <!--Check the url is correct ! And last responce property should be {{}} instead [[]] (as up way data binding) -->
        <iron-ajax
            id="requestRepos"
            url="http://192.168.178.31:8080/demo/assessment" 
            handle-as="json"
            last-response="{{repos}}"
        </iron-ajax>

    </template>
    <script>
        class Assessment extends Polymer.Element {
            static get is() { return 'my-assessment'; }
            static get properties() {
                return {
                    repos: {
                        type : Array,
                        observer : 'isQuestionsLoaded'
                    }
                }
            }
            // set this element's employees property
            constructor() {
                super();
            }

            isQuestionsLoaded(q) {
                if (q) {
                  console.log('loaded questions', q); // questions are loaded.
                 }
                 this.questions = q;
            }

            getAssessment() {
                this.$.requestRepos.generateRequest();
            }    
        }
        window.customElements.define(Assessment.is, Assessment);
    </script>
</dom-module>

my-quiz.html

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="../bower_components/polymer-simple-slider/simple-slider.html">
<link rel="import" href="../bower_components/promise-polyfill/promise-polyfill-lite.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="my-assessment.html">



<dom-module id="my-quiz">
    <template>          

        <my-assessment>
        </my-assessment>
    </template>
    <script>
        class Quiz extends Polymer.Element {
            static get is() { return 'my-quiz'; }
            static get properties() {
                return {
                    index: {
                        type: Number,
                        value: 0
                    } 
                }
            }
            // set this element's employees property
            constructor() {
                super();
            }


        }
        window.customElements.define(Quiz.is, Quiz);
    </script>
</dom-module>

Solution

  • In your case that is shown above the two elements have child parent relation to each other. The element my-quiz contains the element my-assessment. Passing the data up from it's child element can be done with two way data binding. Therefor you will have to add the notify:true to your repos property and use the two way data binding syntax as shown in my example.

    Child elements properties:

     static get properties() {
          return {
               repos: {
                  type : Array,
                  notify: true,
                  observer : 'isQuestionsLoaded'
               }
           }
      }
    

    Parent Element:

    <dom-module id="my-quiz">
      <template>          
          <my-assessment repo-data="{{repos}}"></my-assessment>
      </template>
      <script>
        class Quiz extends Polymer.Element {
            static get is() { return 'my-quiz'; }
            static get properties() {
                return {
                    repoData: {
                        type: Array,
                        observer: '_dataChanged'
                    } 
                }
             }
    
            constructor() {
                super();
            }
            _dataChanged(data) {
               console.log('Data changed:', data)
            }
        }
        window.customElements.define(Quiz.is, Quiz);
      </script>
    </dom-module>
    

    As you can see I have used the two way data binding syntax {{}} to get the data that is send up from your child element. Changes will be observed and logged in the console.

    To read more about this you can take a look in the Polymer documentation