sub-request <iron-ajax>

47 views
Skip to first unread message

remib...@gmail.com

unread,
Apr 5, 2017, 5:08:57 AM4/5/17
to Polymer
Hello,

I would nest 2 Ajax requests.

I have to make a first request to have the list of commands and go looking for the detail of each command with another queries.

But my command displays on outes the records the detail of the last command :(

   <iron-ajax
        auto
        id="requestRepos"
        params='{"ws_key":"XXXXX","filter[current_state]":"9","output_format":"JSON"}'
        handle-as="json"
        last-response="{{response}}">
    </iron-ajax>

    <template is="dom-repeat" items="[[response.orders]]" as="order">
    <iron-ajax
        auto
        id="requestDetail"
        params='{"ws_key":"XXXXXX","output_format":"JSON"}'
        handle-as="json"
        last-response="{{jsondata}}">
    </iron-ajax>    
    <div class="card">
      <div class="circle">{{order.id}}</div>
             <div class="header">{{jsondata.order.total_paid_real}} €</div>
             </div>
        </template>

Can we nest 2 <iron-ajax>?

Daniel Llewellyn

unread,
Apr 6, 2017, 12:07:15 PM4/6/17
to remib...@gmail.com, Polymer
​Nesting is not your issue.

The issue is that you are assigning the output of each repeated iron-ajax into the same variable (jsondata). This obviously means that only the last result to be returned is displayed on all instances. The best way to work this is to extract the content of your dom-repeat into a separate element that takes a an order id as input and displays the card associated with that one order id. Then include that element in your dom-repeat so that there is no shared-state:

<template is="dom-repeat" items="[[orders]]" as="order">
    <my-order-element order-id="[[order.id]]"></my-order-element>
</template>​

Then your element includes something along the lines of:

<dom-module is="my-order-element">
    <template>
        <iron-ajax auto id="requestDetail" handle-as="json"
            params='{"ws_key":"xxxxxx","output_format":"JSON"}'
            last-response="{{orderJson}}">
        </iron-ajax>
        <div class="card">
            <div class="circle">[[orderId]]</div>
            <div class="header">[[orderJson.order.total_paid_real]] &euro;</div>
        </div>
    </template>
    <script>
        Polymer({is: 'my-order-element'});
    </script>
</dom-module>


--
Daniel Llewellyn
Bowl Hat
Reply all
Reply to author
Forward
0 new messages