简体   繁体   中英

How to dynamically populate iron-list elements

So I have an iron-list element for a user's data history. The iron-list is not part of a custom element. It is simply on the page. I want to populate once the user has successfully logged in. Perhaps it is just my inexperience with polymer, but there doesn't seem to be a straightforward way to do this. First attempt (simplified for reading, eg I don't actually use jquery, there's lots of error-handling code I'm omitting, etc):

<iron-list as="item" style='height: 100%;' id='history-list'>
  <template>
    <div style='min-height: 140px;'>
      <ul>
        <!-- various fields for each record as list items -->
      </ul>
    </div>
  </template>
</iron-list>
<script>
  //once user is logged in
  var items = $.getJSON('userhistoryscript');
  //setAttribute doesn't work either
  document.getElementById('history-list').items = items;
</script>

I would swear this worked in an earlier version of Polymer. But it doesn't seem to work now, which is fine, but I need an alternative.

Some alternatives I've considered:

  1. Have iron-ajax element in same DOM scope and set ' the URL once the user is logged in to trigger the xhr request. I'm not sure whether or not that'd work.

  2. Wrap the list in a custom element and use an iron-meta-query per chrisW's answer.

Those options are terrible . I cannot believe there is no simpler way to accomplish this feat. How do I conditionally fetch data based on user input and dynamically add an iron-list to the page (or update one that's already there)? Is there really no API for this use case?

UPDATE

Thank you for your answers. Turns out that my original code actually works fine: it was actually a build process issue. For some reason iron-list did not get installed when I installed the project dependencies through bower. I took out the vulcanized import (which must not have contained a ref to iron-list either) and imported all the elements directly, then I got the 404 and figured out what had happened.

I think that for best practices, you should use this.$.historyList to refeer id on this element. Anyway, when you get data to populate iron-list you should use this.set('items', data); An example using your element looks like:

<iron-list>
    <template is="dom-repeat" items="{{data}}" as="history">
        <!--history.property-->
    </template>
 </iron-list>

<script>
  Polymer({
    properties:{
        data:{type:Array, value:[],}
    },
    _functionToSetDataWhenUserIsLoggedIn: function(data){
        this.set('data',data);
    }
  });
</script>

Edit

An example of iron-list

<template is="dom-bind">
  <iron-ajax url="data.json" last-response="{{data}}" auto></iron-ajax>
  <iron-list items="[[data]]" as="item">
    <template>
      <div>
        Name: <span>[[item.name]]</span>
      </div>
    </template>
  </iron-list>
</template>

This example is using an ajax call that executes automatically and populates the iron-list without the need to create a customized element.

More about iron-list on:
https://elements.polymer-project.org/elements/iron-list

I didn't entirely understand your question. Hope this helps.

  <iron-list items="[[data]]" as="item">
    <template>
      <div>
        Name: <span>[[item.name]]</span>
      </div>
    </template>
  </iron-list>   

    properties:{
        data:{type:Array, value:[],}
    },
    // the attached function is automatically called 
    attached: function() {
       // Use an iron meta in the element that you keep track in of login information
       // or create an onLogin listener
       var isLoggedIn = new Polymer.IronMetaQuery({key: 'isLoggedIn'}).value, 
       if (isLoggedIn) {
          var jsonData = $.getJSON('userhistoryscript'); 
          this.set('data',jsonData);
       }
    }

Side note, when access elements by ids in Polymer elements, make sure you do it this way:

this.$.elementId

or

Polymer.dom('#elementId')

Edit since you don't want to create a custom polymer element

Source Code

  <template is="dom-bind">
    <iron-list id="list">

    </iron-list>  

  </template>

  <script>
    document.addEventListener('onLogin', function(event) {
      var list = document.getElementById('#list');
      var jsonDataObjects = $.getJSON('userhistoryscript'); 
      for (var i = 0; i < jsonDataObjects.length; i++) {
        var div = document.createElement('div');
        div.textContent = jsonDataObjects[i].info; // change this line
        list.appendChild(div);
      }

    });
  </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM