Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Polymer indeterminate paper-progress when iron-ajax is loading

I am trying to show a paper-progress when an iron-ajax request is in progress with no success. Below is the code for a custom element get-products-service which hosts the iron-ajax request, and a products-list which hosts the paper-progress.

This is the whole products-list dom-module:

<dom-module id="product-list">
  <style>
    :host {
      display: block;
      width: 100%;
      text-align: center;
    }
    product-card {
      margin-left: 10px;
      margin-bottom: 30px;
    }
</style>
<template>
  <template is="dom-if" if="{{loading}}">
    <paper-progress value="10" indeterminate="true"  style="width:100%;"></paper-progress>
  </template>
  <paper-button id="previous" on-tap='previousPage'>Previous</paper-button>
  <paper-button id="next" on-tap='nextPage'>Next</paper-button>

  <get-products-service products="{{products}}" id="productservice"   page="{{page}}" loading="{{loading}}"></get-products-service>
 <div>
    <template is="dom-repeat" items="{{products}}">
     <product-card on-cart-tap="handleCart" product="{{item}}">
       <img width="100" height="100">
       <h3>{{item.name}}</h3>
       <h4>{{item.display_price}}</h4>
     </product-card>
    </template>
  </div>
</template>
</dom-module>
<script>
  (function() {
  Polymer({
  is: 'product-list',

  properties: {
    page: {
      type: Number,
      notify: true,
      value: 1
    }
  },
  handleCart: function(event) {

  },
  previousPage: function(event){
   this.page = --this.page;
   console.log("page: "+this.page);
  },
  nextPage: function(event){
    this.page = ++this.page;
    console.log("page: "+this.page);
  }
});
})();
</script>

This is the whole get-products-service

<dom-module id="get-products-service">
  <style>
    :host {
     display: none;
    }
  </style>
<template>
  <iron-ajax id="productsajax"
    url="http://localhost:3003/api/products"
    params='{"token":"mytoken"}'
    method='GET'
    on-response='productsLoaded'
    handleAs="json"
    loading="{{loading}}" >
  </iron-ajax>
  </template>
</dom-module>

<script>
 (function() {

   Polymer({is: 'get-products-service',

     properties: {
       products: {
       type: Array,
       notify: true
     }, 
    page: {
      type: String,
      notify: true,
    },
    perpage: {
      type: String,
      readOnly: true,
      value: "6"
    },
    loading: {
      type: Boolean,
      readOnly: true,
      notify: true,
      value: false
    }
  },

productsLoaded: function(request) {
  console.log(this.$.productsajax.lastResponse);
  responseObject = this.$.productsajax.lastResponse;
  this.products = responseObject.products;
},

ready: function(){
  this.$.productsajax.params.page = this.page;
  this.$.productsajax.params.per_page = this.perpage;
},
observers: [
          'attributesReady(page)'
        ],

attributesReady: function(page) {
    this.page = page;
    this.$.productsajax.params.page = page;
    this.$.productsajax.params.per_page = this.perpage;
    console.log("service page: "+page);
    this.async(function() {
        this.$.productsajax.generateRequest();
      }, 3000);
  }
 });
})();
</script>
like image 795
Optimus Pette Avatar asked Jun 11 '15 19:06

Optimus Pette


Video Answer


3 Answers

Your best option is to separate your concerns. First, your get-products-service:

<dom-module id="get-products-service">
  <style>
    :host {
      display: none;
    }
  </style>
  <template>
    <iron-ajax id="productsajax"
      url="http://localhost:3003/api/products"
      method='GET'
      loading="{{loading}}"
      handleAs="json">
    </iron-ajax>
  </template>

  <script>
    Polymer({
      is: 'get-products-service',

      properties: {
        loading: {
          type: Boolean,
          readOnly: true,
          notify: true,
          value: false
        }
      }
    });
  </script>
</dom-module>

Then, in your product-list:

<template>
  <template is="dom-if" if="{{loading}}">
    <paper-progress value="10" indeterminate="true" style="width:100%;"></paper-progress>
  </template>
  <get-products-service loading="{{loading}}"></get-products-service>
</template>

This way get-products-service and paper-progress don't have to know anything about each other, and should be more composable elsewhere in your application.

like image 190
Zikes Avatar answered Nov 15 '22 03:11

Zikes


Finally I was able to have this working using a combination of @Zikes and @Scott answers. First I seperated the concerns as suggested by @Zikes so i put this in product-list element.

<template>
  <template is="dom-if" if="{{loading}}">
    <paper-progress value="10" indeterminate="true" style="width:100%;"></paper-progress>
  <template>
  <get-products-service loading="{{loading}}"></get-products-service>
</template>

In the get-products-service element is where i had to make some changes to have it working. For some reason, i wouldn't get the loading property from the iron-ajax set to true. Therefore @Zikes answer wouldn't work as it was. I had to make a few changes:

  1. Explicitly set the value of the loading property to true just before the iron-ajax request is fired.
  2. Explicitly set the value of loading to false when the on-response event is fired.
  3. For this to work I changed @Zikes answer's loading property by removing readOnly: true; line.

Using @Scott suggestion, I slowed things down by using:

this.async(function() {
        this.$.productsajax.generateRequest();
      }, 5000);

to get a chance to see the progress-bar .

The resulting get-products-service element looks like this:

<dom-module id="get-products-service">
  <style>
    :host {
      display: none;
    }
  </style>
  <template>
    <iron-ajax id="productsajax"
      url="http://localhost:3003/api/products"
      method='GET'
      on-response='productsLoaded'
      handleAs="json">
    </iron-ajax>
  </template>
</dom-module>

<script>
  Polymer({is: 'get-products-service',
    properties: {
     loading: {
      type: Boolean,
      notify: true,
      value: false
      }
    },
    //on-response callback
     productsLoaded: function(request) {
         this.loading = false;
     },

   //An observer method where ajax request is generated
    attributesReady: function(page) {
      //set the value of loading to true just before generating request
      this.loading = true;
      //slow things to get a chance to see the progress-bar
      this.async(function() {
        this.$.productsajax.generateRequest();
      }, 5000);
    }

  });
</script>

I believe @Zikes answer should work too and I'm the one who failed to implement it correctly so I will accept it as the correct answer.

like image 33
Optimus Pette Avatar answered Nov 15 '22 02:11

Optimus Pette


@Zikes has the right answer. I'm supplementing here with a fully working implementation (only for Chrome, x-platform requires a few tweaks):

<!doctype html>
<html>
<head>

  <meta charset="utf-8">

  <base href="http://milestech.net/components/">

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

</head>
<body>

  <use-service></use-service>

  <dom-module id="get-products-service">
    <template>

      <iron-ajax url="iron-ajax/bower.json" auto="{{go}}" handle-as="text" last-response="{{products}}"></iron-ajax>

    </template>
    <script>

      Polymer({

        properties: {
          products: {
            notify: true,
            value: null
          }
        },

        ready: function() {
          // this bit just because otherwise it's too fast to see
          this.async(function() {
            this.go = true;
          }, 1000);
        }
      });

    </script>
  </dom-module>

  <dom-module id="use-service">
    <template>

      <template is="dom-if" if="{{!products}}">
        <paper-progress value="10" indeterminate="true" style="width:100%;"></paper-progress>
      </template>

      <get-products-service products="{{products}}"></get-products-service>

      <pre>{{products}}</pre>

    </template>
    <script>

      Polymer({});

    </script>
  </dom-module>

</body>
</html>
like image 27
Scott Miles Avatar answered Nov 15 '22 03:11

Scott Miles