在Polymer中,是否可以使用一组参数来进行iron-ajax调用?

时间:2016-01-08 06:34:31

标签: ajax polymer polymer-1.0 polymer-starter-kit

所以我想象的是一个包含id列表的参数数组,让我们调用这个Array1。这些id将用作对同一URL的ajax调用的参数。当它进行调用时,我想将一些信息从ajax调用推送到公共数组(Array2),然后转到Array1中的下一个id。这可以用Polymer实现吗?如果是这样,我从哪里开始?

代码:

        <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>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,我认为这会奏效。它可能不是最有效的方法,但可以完成工作。

<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>

添加函数handleReponse

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

根据jdepypere的建议进行编辑(无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添加了GenerateRequest方法(正在运行)

  <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;
        },