Coulton Fraser Coulton Fraser -4 years ago 175
JSON Question

Polymer JS – template repeat does nothing with my array. (Chrome)

This does nothing in my code..

<template repeat="{{item in list}}"> </template>


I have tried to also use as below but I get errors with this method:

<template is="dom-repeat" id="example" items="{{list}}"></template>


And i get the following errors with
is="dom-repeat"
:

Uncaught TypeError: Polymer.dom.addDebouncer is not a function
Uncaught TypeError: Polymer.dom is not a function


Why?

Here is my code

<link rel="import" href="../lib/iron-ajax/iron-ajax.html">

<link rel="import" href="../lib/polymer/polymer.html">

<link rel="import" href="welcome-title.html">

<dom-module id="remove-user">

<template>
<iron-ajax id="getAll" url="http://localhost:3002/secure/api/all" method="GET" handle-as="json" on-response="getAllCB" with-credentials='true'></iron-ajax>

<div class="ui relaxed stackable grid centered" id="admin-container">
<welcome-title class="ui center aligned row grid"></welcome-title>
<form class="ui grid remove-user hide twelve wide column" method='post' action="/secure/add-user">
<h3>Remove User</h3>

<table class="ui unstackable celled table ">
<thead>
<tr><th class="nine wide">Username</th>
<th class="three wide">Permission</th>
<th class="one wide"></th>
</tr>
</thead>
<tbody> ...


Here is where I am confused. I have simplified the loop to make it easier to debug but it doesn't appear. Although any text outside of the template repeat appears. There are no errors in the console.

<template repeat="{{user in users}}">
<span>{{user}}</span>
</template>


Why does nothing inside the repeat not show up on my page?

... <tr>
<td><span></span></td>
<td></td>
<td class="collapsing">
<div class="ui fitted checkbox">
<input type="checkbox"> <label></label>
</div>
</td>
</tr>

</tbody>
<tfoot class="full-width">

<tr><th colspan="3">

<button class="right floated negative ui button"><i class="remove user icon"></i>Remove User(s)</button>
</th>
</tr></tfoot>
</table>
</form>
</div>

</template>



</dom-module>
<script>

Polymer({

is: "remove-user",

ready: function(){

this.$.getAll.generateRequest();

},

getAllCB: function(data){

this.users = data.detail.response;
}

});

</script>


The users JSON object looks like this when outputting to browser console using JSON.stringify():

[{"username":"admin","permission":"admin"},
{"username":"admin","permission":"application1"},
{"username":"user","permission":"application1"},
{"username":"test","permission":"application1"}]


Access to the complete project:
The file in question is under
authentication/public/elements/remove-user.html

The main page that loads this elements is
authentication/secure.html

https://github.com/CoultonF/Web-Development-3-Node.JS

Answer Source

It looks like you're attempting to use an expression inside a data binding, which Polymer 1.x does not support (nor is it on the roadmap for 2.x).

The correct form of this code:

<template repeat="{{user in users}}">
  <span>{{user}}</span>
</template>

is this:

<template is="dom-repeat" items="{{users}}" as="user">
  <span>{{user}}</span>
</template>

HTMLImports.whenReady(() => {
  Polymer({
    is: 'x-foo',
    properties: {
      users: {
        type: Array,
        value: () => ['Andy', 'Bob', 'Charlie']
      }
    }
  });
});
<head>
  <base href="https://polygit.org/polymer+1.7.1/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="polymer/polymer.html">
</head>
<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <template is="dom-repeat" items="{{users}}" as="user">
        <span>{{user}}</span>
      </template>
    </template>
  </dom-module>
</body>

codepen

You can read more about the dom-repeat template in the Polymer docs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download