Search code examples
ajaxpolymerpolymer-1.0polymer-starter-kit

In Polymer, is it possible to use an array of parameters to make an iron-ajax call?


So what I am picturing is an array of parameters that I have which contain a list of id's, let's call this Array1. These id's are going to be used as parameters for an ajax call to the same url. When it makes the call I want to push some info off of the ajax call to a common array (Array2) and then move on to the next id in Array1. Is this possible to implement with Polymer? And if so where do I begin?

Code:

        <iron-ajax id="ajax"
               auto="false"
                url='http://url.com/details?Id='
                handle-as="json"
                on-response="handleResponse"></iron-ajax>



<script>
    var playerData = [];
    Polymer({
        is: 'my-team',
        properties: {
        },
        attached: function () {
            for (var i = 0; i < array.length; i++) {
                this.$.ajax.url = 'http://url.com/details?Id=' + currentTeam[i];
                console.log(array[i]);
            }
        },
        handleResponse: function (r) {
            // do something;
            playerData.push(r.detail.response);
        }
    });
</script>

Solution

  • If I understood correctly, I think this will work. It might not be the most efficient way of doing it, but gets the job done.

    <template is="dom-repeat" items="{{array1}}" as="id">
      <iron-ajax auto
                 url='http://website.com/{{id}}'
                 handle-as="json"
                 on-response="handleResponse">
       </iron-ajax>
    </template>
    

    Add a function handleReponse

     handleResponse: function(r) {
          var response = r.detail.response;
          // do something;
        },
    

    Edit based on jdepypere's suggestion (No Dom-Repeat):

      <iron-ajax id="ajax" url='http://website.com/'
                 handle-as="json"
                 on-response="handleResponse">
       </iron-ajax>
    
    attached: function() {
        for (var i = 0; i < this.array1.length; i++) {
           this.$.ajax = 'http://website.com/' + this.array1[i];
        }
    },
    handleResponse: function(r) {
              var response = r.detail.response;
              // do something;
    },
    

    Edit2 added GenerateRequest method (Now Working):

      <iron-ajax id="ajax" url='http://website.com/'
                     handle-as="json"
                     on-response="handleResponse">
           </iron-ajax>
    
     properties: {
              array1: {
                value: ['123', '123555', '235']
              }
            },
            attached: function() {
              console.log('attached');
              for (var i = 0; i < this.array1.length; i++) {
                console.log(i);
                this.$.aj.url = 'http://website.com/' + this.array1[i];
                this.$.aj.generateRequest();
              }
            },
            handleResponse: function(r) {
              var response = r.detail.response;
              console.log('handle');
              // do something;
            },