javascripthtmlfirebasepolymerpolymerfire

How can I loop through an array of keys and do query for each using firebase-query in a declarative manner?


As title suggests, I am trying to use firebase-query element to do query for an array of keys which in return I should get bunch of objects per each key from the query. This can easily be done in java or swift , but when it comes to polymer elements I can't figure it out.

I have tried putting firebase-query in a dom-repeat but doesn't seem to work. I also tried changing the equal-to key which it doesn't seem to initiate the query for the new key.

See below for code :

<!doctype html>
<html>
<head> 
</head> 
<body>
  <template is="dom-bind" id="scope">

       <!-- .......other code -->
      <!-- condKey is from the array I need to loop -->
      <!--through and array comes from another query-->

      <firebase-query
        id="myQuery"
        app-name="ProjectName"
        order-by-child="childName" 
        path = "/nodeKey"   
        equal-to=[[condKey]]  
        data="{{results}}">
      </firebase-query>

    <!-- ***data comes from another query -->

    <div id="listContainer">       
      <template is="dom-repeat" items=[[data]] as="item">    
        <div class="item">
        <div class$="{{_computeItems(item.key)}}"></div>
          <template is="dom-repeat" items={{results}} as="myresult">
            <div>[[myresult.key]]</div>
          </template>
        </div>    
      </template>
    </div>  


    <script> 
    scope._computeItems =  function(key) {
          console.log("key of query is: " , key); 
          scope.condKey = key ;                 
        }; 
    </script>
    </template> 

    <!-- .......other code -->
    </body>

Solution

  • So I was trying to achieve this using Polymer data binding and by nesting children that each do it's own query to get what it needs and I was a rookie back then. Here is how to do it easily via Polymer data binding and in a declarative manner.

    <parent-element>
      <template is="dom-repeat" items="[[keyArrays]]">
            <child-element key="[[item]]"></child-elements>
      </template>
    </parent-element> 
    

    and then in each child-element you do a query via firebase-query in child-element definition:

    <dom-module id="child-element">
    
        <template>
         ....
    
         <firebase-query path="/path/[[key]]" data="{{_queryResult}}></firebase-query>
    
         <element-to-bind-result-with data="[[_queryResult]]"></element-to-bind-result-with>
    
        </template>
          ....
    <dom-module>
    

    Hope it helps someone.