0

单击此处获取 jsBin

例如,我想计算屏幕宽度并将结果分配给一个介于 0 和 3 之间的数字。我正在尝试使用它iron-media-query来做到这一点。

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

我究竟做错了什么?

仅供参考,解决此问题后,我计划在queryResult()方法中添加一个条件来存储indexwhen queryMatchesis的值true

例如
queryResult: function() {
  ...
  if(this.queryMatches) {
    this.set('mediaWidth', index);
  }
  ...
}
http://jsbin.com/kamusivebi/1/edit?html,控制台,输出
<!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>
4

1 回答 1

2

两个问题:

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

  2. 您的演示缺少对 的 HTML 导入<iron-media-query>,因此queryMatches总是如此undefined

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

<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>

密码笔

于 2016-11-10T23:57:23.480 回答