0

jsBin については、ここをクリックしてください

たとえば、画面の幅を計算し、その結果を 0 と 3 の間の数値に割り当てたいとします。私はこれを行うために使用しようとしiron-media-queryています。

trueクエリの 1 つが の値を返し、他の 3 つのクエリが を返す (コンソールに記録される) ことを期待していますfalse。ただし、それらはすべて の値を返しますundefined

私は何を間違っていますか?

参考までに、これを解決した後、 when isqueryResult()の値を格納するメソッドに条件を追加する予定です。indexqueryMatchestrue

例えば
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>
4

1 に答える 1

2

2 つの問題:

  1. iron-media-queryそれぞれの結果を単一のプロパティに誤ってバインドしているqueryMatchesため、各反復で前の反復の結果が上書きされます。itemこれを修正するには、反復子のサブプロパティ (例: ) にバインドできます。これには、 の型を配列から配列にitem.queryMatches変更する必要があります。次に、に渡すことができます。queriesStringObjectitem.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 に答える