Polymer 1.x:在dom-repeat中应用iron-media-query来确定屏幕宽度

时间:2016-11-10 22:37:26

标签: polymer polymer-1.0 jsbin polymer-1.x dom-repeat

Click here for jsBin

我想计算屏幕宽度,并将结果分配给例如0到3之间的数字。我正在尝试使用iron-media-query来执行此操作。

我希望看到(登录到控制台)其中一个查询返回值true,其他三个返回false。但是,它们都返回值undefined

我做错了什么?

仅供参考,在我解决此问题后,我计划在queryResult()方法中添加条件,以便在indexqueryMatches时存储true的值。

例如
queryResult: function() {
  ...
  if(this.queryMatches) {
    this.set('mediaWidth', index);
  }
  ...
}
http://jsbin.com/kamusivebi/1/edit?html,console,output
<!doctype html>
<head>
  <meta charset="utf-8">
  <base href="https://polygit.org/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link href="polymer/polymer.html" rel="import">
</head>
<body>

<dom-module id="x-element">

<template>
  <style></style>

  <template is="dom-repeat"
            id="page"
            items="[[queries]]"
            >
    <iron-media-query id="query"
                      full
                      query="[[item]]"
                      query-matches="{{queryMatches}}"
                      >
      <span hidden>{{queryResult(index)}}</span>
    </iron-media-query>
  </template>

</template>

<script>
  (function(){
    Polymer({
      is: "x-element",
      properties: {
        queryMatches: Boolean,
        queries: {
          type: Object,
          value: function() {
            return [
              '(max-width:  699px)'                         ,
              '(min-width:  700px) AND (max-width:  899px)' ,
              '(min-width:  900px) AND (max-width: 1124px)' ,
              '(min-width: 1125px)'                         ,
            ];
          },
        },
      },
      queryResult: function(index) {
        this.set('mediaWidth', index);
        console.log('mediaWidth', this.mediaWidth);
        console.log('queryMatches', this.queryMatches);
      },
    });
  })();
</script>

</dom-module>

<x-element></x-element>

</body>

1 个答案:

答案 0 :(得分:2)

两个问题:

  1. 您错误地将每个iron-media-query的结果绑定到单个queryMatches属性,因此每次迭代都会覆盖上一次迭代的结果。要更正此问题,您可以绑定到item迭代器的子属性(例如,item.queryMatches),这需要将queries的类型从String数组更改为一个Object数组。然后,您可以将item.queryMatches传递给queryResult()

  2. 您的演示缺少<iron-media-query>的HTML导入,因此queryMatches始终为undefined

  3. 这是一个带有更正的演示:

    <head>
      <meta charset="utf-8">
      <base href="https://polygit.org/polymer+:1.7.0/components/">
      <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
      <link href="polymer/polymer.html" rel="import">
      <link href="iron-media-query/iron-media-query.html" rel="import">
    </head>
    <body>
      <dom-module id="x-element">
        <template>
          <template is="dom-repeat"
                    id="page"
                    items="[[queries]]"
                    >
            <iron-media-query id="query"
                              full
                              query="[[item.q]]"
                              query-matches="{{item.queryMatches}}"
                              >
            </iron-media-query>
            <span hidden>[[queryResult(index, item.queryMatches)]]</span>
            <div>[[item.q]]: [[item.queryMatches]]</div>
          </template>
        </template>
    
        <script>
          HTMLImports.whenReady(function() {
            Polymer({
              is: 'x-element',
              properties: {
                queries: {
                  type: Object,
                  value: function() {
                    return [
                      { q: '(max-width:  699px)'                         },
                      { q: '(min-width:  700px) AND (max-width:  899px)' },
                      { q: '(min-width:  900px) AND (max-width: 1124px)' },
                      { q: '(min-width: 1125px)'                         },
                    ];
                  },
                },
              },
              queryResult: function(index, queryMatches) {
                console.log('index', index, 'queryMatches', queryMatches);
              }
            });
          });
        </script>
    
      </dom-module>
    
      <x-element></x-element>
    
    </body>

    codepen