Vanita
Vanita

Reputation: 732

Retrieving JSON sub-array using Polymer iron-ajax

I am trying to retrieve a JSON file using iron-ajax, in order to create a navigation accordion menu. So far, this reads in the "top-level" headers fine, but I'm struggling to get the sub-menu names returned.

I am wondering if I need a nested template dom-repeat in the nav-head element, but have been unsuccessful in getting it to work so far.

Sample of JSON:

{
 "headers": [
  {
   "name": "Header One",
    "sub": [
     {
      "name": "Sub Heading One"
     },
     {
      "name": "Sub Heading Two"
     }
    ]
   }
  ]
 }

Polymer Code:

<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="nav-head.html">
<link rel="import" href="nav-sub.html">

<dom-module id="nav-accordion">
  <template>
    <style include="shared-styles">
    </style>

<iron-ajax auto
      url="../../../api/nav.json"
      handle-as="json"
      last-response="{{ajaxResponse}}"></iron-ajax>

<template is="dom-repeat" items="[[ajaxResponse.headers]]">
  <div class="horizontal-section">
    <nav-head heading=[[item.name]]></nav-head>
  </div>
</template>

</template>

<script>
(function() {
  'use strict';

  Polymer({
    is: 'nav-accordion',

    properties: {
    }

  });
})();

Upvotes: 0

Views: 853

Answers (1)

Vanita
Vanita

Reputation: 732

This is actually quite simple to do. Find code below:

<template is="dom-repeat" items="{{ajaxResponse.headers}}" as="header">
  <div class="horizontal-section">
    <nav-head heading={{header.name}}>
      <template is="dom-repeat" items="{{header.sub}}" as="sub">
        <nav-sub subheading={{sub.name}}></nav-sub>
      </template>
    </nav-head>
  </div>
</template>

The important thing to remember is to fetch the sub arrays using 'header.sub', not 'ajaxResponse.sub'

Upvotes: 2

Related Questions