用铁 - 艾哈克斯的递归呼叫



我怀疑我在这里离去,但是从本质上讲,我正在尝试检索机票清单,然后是每张票的详细信息。我有第一部分工作,但是当我尝试添加"递归"部分时,它只是失败了。

这是仅列出IDS的工作代码:

<template is="dom-bind" id="app">
    <iron-ajax auto
               url="https://<mydomain>.freshdesk.com/api/v2/tickets"
               headers='{ "user": "<apikey>", "pass": "X", "sendImmediately": "true" }'
               handle-as="json"
               method="GET"
               last-response="{{ticketList}}"
               with-credentials></iron-ajax>
    <template is="dom-repeat" items="[[ticketList]]">
        <div class="ticket">{{item.id}}</div>
    </template>
</template>

我尝试了一些使用不同URL("/tickets/" + {{item.id}})的模板中新的iron-ajaxtemplate,但我认为这甚至与正确的方法相关。我在DOM中获得的只是具有#document-fragment

的模板元素

那么,如何获得/票/20,/tickets/21等的详细信息?

可以在dom-repeat模板中使用iron-ajax声明地执行请求(如您所提到的)。

下面的示例检索包含polymer一词的书籍列表,然后检索到每本书的链接。

<!doctype html>
<html>
<head>
  <meta name="description" content="https://stackoverflow.com/questions/37817472">
  <meta charset="utf-8">
  <base href="http://polygit.org/components/">
  <script href="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link href="polymer/polymer.html" rel="import">
  <link href="iron-ajax/iron-ajax.html" rel="import">
</head>
<body>
  <dom-module id="my-el">
    <template>
      <!-- get list of book titles that include the word "polymer" -->
      <iron-ajax id="ajax"
                 url="https://www.googleapis.com/books/v1/volumes?q=polymer"
                 handle-as="json"
                 last-response="{{response}}" auto></iron-ajax>
      <template is="dom-repeat" items="[[response.items]]">
        <!-- get links to the list of books from previous iron-ajax --> 
        <iron-ajax id="ajax"
                   url="{{url(item.id)}}"
                   handle-as="json"
                   on-response="onResponse"
                   auto></iron-ajax>        
      </template>
    </template>
    <script>
      Polymer({
        is: 'my-el',
        properties: {
          response: {
            type: String,
            notify: true
          }
        },
        onResponse: function(e) {
          var p = document.createElement('p');
          p.textContent = e.target.lastResponse.selfLink;
          Polymer.dom(this.root).appendChild(p);
        },
        url: function(id) {
          return "https://www.googleapis.com/books/v1/volumes/" + id;
        }
      });
    </script>
  </dom-module>
  <my-el></my-el>
</body>
</html>

这是同一示例的JS垃圾箱。如果您尝试运行太多...

,您可能会得到403

http://jsbin.com/jijehus/5/edit?html,output

update

A1626询问您是否可以使用一个iron-ajax元素来处理所有请求。这也是可能的。

<!doctype html>
<html>
<head>
  <meta name="description" content="https://stackoverflow.com/questions/37817472">
  <meta charset="utf-8">
  <base href="https://polygit.org/components/">
  <script href="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link href="polymer/polymer.html" rel="import">
  <link href="iron-ajax/iron-ajax.html" rel="import">
</head>
<body>
  <dom-module id="my-el">
    <template>
      <iron-ajax id="ajax"
                 url="https://www.googleapis.com/books/v1/volumes?q=polymer"
                 handle-as="json"
                 on-response="onResponse"
                 last-response="{{response}}" auto></iron-ajax>
    </template>
    <script>
      Polymer({
        is: 'my-el',
        properties: {
          response: {
            type: Object,
            notify: true
          },
          queue: {
            type: Array,
            notify: true,
            value: function() { return []; }
          }
        },
        onResponse: function(e) {
          var ajax = this.$.ajax;
          var originalUrl = 'https://www.googleapis.com/books/v1/volumes?q=polymer';
          var url = ajax.lastRequest.xhr.responseURL;
          if (url.includes(originalUrl)) {
            console.log('this is the first request');
            for (var i = 0; i < ajax.lastResponse.items.length; i++) {
              this.push('queue', ajax.lastResponse.items[i].id);
            }
            ajax.url = this.url(this.pop('queue'));
          } else {
            console.log(ajax.lastResponse.selfLink);
            ajax.url = this.url(this.pop('queue'));
          }
        },
        url: function(id) {
          return "https://www.googleapis.com/books/v1/volumes/" + id;
        }
      });
    </script>
  </dom-module>
  <my-el></my-el>
</body>
</html>

https://jsbin.com/beyawe/edit?html,Console

最新更新