Polymer 1.x: Applying iron-media-query inside dom-repeat to determine screen width -
i want calculate screen width , assign result number between, say, 0 , 3, example. trying use iron-media-query this.
i expect see (logged console) 1 of queries return value of true , other 3 return false. however, return value of undefined.
what doing wrong?
fyi, after solve this, plan add conditional queryresult() method store value of index when querymatches 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) , (max-width: 899px)' , '(min-width: 900px) , (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>
two problems:
you're incorrectly binding results of each
iron-media-querysinglequerymatchesproperty, each iteration overwrite results of previous iteration. correct this, bind sub-property ofitemiterator (e.g.,item.querymatches), requires changing type ofqueriesstringarrayobjectarray. then, passitem.querymatchesqueryresult().your demo missing html import
<iron-media-query>,querymatchesundefined.
here's demo corrections:
<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) , (max-width: 899px)' }, { q: '(min-width: 900px) , (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>
Comments
Post a Comment