Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Expression in polymer
Since Polymer doesn't support expression directly, I create an express function as expression alternative.

Demo

Demo source

  1. <html>
  2. <head>
  3.     <title>Express function for Polymer</title>
  4.     <base href="https://cdn.rawgit.com/download/polymer-cdn/2.0.2/lib/">
  5.     <link rel="import" href="polymer/polymer.html" />
  6. </head>
  7. <body>
  8.     <h1>Expression</h1>
  9.     <expressed-content count="15"></expressed-content>
  10.  
  11.     <dom-module id="expressed-content">
  12.         <template>
  13.             <template is="dom-repeat" items="[[sequences]]" as="item">
  14.                 <!-- This means: -->
  15.                 <!-- 1. sqrt(item) as $result -->
  16.                 <!-- 2. printText($result, item) as output -->
  17.                 <div>[[express(item, sqrt, printText, item)]]</div>
  18.             </template>
  19.         </template>
  20.         <script>
  21.             'use strict';
  22.             class ExpressedContent extends Polymer.Element {
  23.  
  24.                 static get is() { return 'expressed-content'; }
  25.  
  26.                 static get properties() {
  27.                     return {
  28.                         count: {
  29.                             type: Number,
  30.                             value: 10,
  31.                             observer: '_countChanged'
  32.                         },
  33.  
  34.                         sequences: {
  35.                             type: Array
  36.                         }
  37.                     }
  38.                 }
  39.  
  40.                 _countChanged(n, o) {
  41.                     n = Math.max(0, n * 1);
  42.                     let sequences = [];
  43.                     for (let i = 0; i < n; i++) {
  44.                         sequences.push(+ 1);
  45.                     }
  46.                     this.sequences = sequences;
  47.                 }
  48.  
  49.                 /**
  50.                  * An express function instead of expression
  51.                  */
  52.                 express() {
  53.                     let args = [...arguments];
  54.  
  55.                     // first argument should be first parameter
  56.                     let params = [args.shift()];
  57.                     // second argument should be a function
  58.                     let fn = args.shift();
  59.  
  60.                     // arguments before next function will be additional parameters
  61.                     while (args[0] && typeof (args[0]) !== 'function') {
  62.                         params.push(args.shift());
  63.                     }
  64.  
  65.                     // execute and receive result
  66.                     let nextMainParam = fn.call(this, ...params);
  67.  
  68.                     // continue express when args is not empty
  69.                     return args.length === 0 ? nextMainParam : this.express(nextMainParam, ...args);
  70.                 }
  71.  
  72.                 sqrt(n) {
  73.                     return n * n;
  74.                 }
  75.  
  76.                 printText(n1, n2) {
  77.                     return `${n2} x ${n2} = ${n1}`;
  78.                 }
  79.  
  80.             }
  81.             customElements.define(ExpressedContent.is, ExpressedContent);
  82.         </script>
  83.     </dom-module>
  84. </body>
  85. </html>