【问题标题】:Polymer switch between nested templates via 2-way binding通过 2 路绑定在嵌套模板之间切换聚合物
【发布时间】:2015-03-25 14:37:54
【问题描述】:

我有一个聚合物元素,它有条件模板要根据称为“大小”的聚合物变量进行渲染

<template>
    <!-- icon fonts. Please do not move -->
    <link rel="stylesheet" href="meteocons/css/fontello.css">

    <template if="{{size == 'half'}}">
        <div fit layout center vertical>
            <h1 class="weather-value">{{temperature.F}}&deg;{{unit}}</h1>
            <i class="{{icon_single}} weather-icon-single"></i>
            <h3 class="weather-location">{{city}}, {{state}}</h3>
        </div>
    </template>

    <template if="{{size == 'triple'}}">
        <div fit layout center vertical>
            <h1 class="weather-value">{{temperature.F}}&deg;{{unit}}</h1>
            <i class="{{icon_single}} weather-icon-single"></i>
            <h3 class="weather-location">{{city}}, {{state}}</h3>
        </div>
    </template>

    <template if="{{size == 'quadro'}}">
        <div class="nam-weather-logo"><h5>NAM-weather</h5></div>
        <div class="forecast-time"><h5>{{currTime}}</h5></div>
        <div layout horizontal center-justified>
            <h3 class="weather-location">{{city}}, {{state}}</h3>
        </div>
        <div layout horizontal center-justified><h5 class="weather-sub-title">5-Day Weather Forecast</h5></div>
        <div layout horizontal center-justified>
            <template repeat="{{day in forecast}}">
                <div layout vertical center class="forecast-day">
                    <h1 class="weather-value-forecast">{{day.day}}</h1>
                    <i class="{{day.weather_icon}} weather-icon forecast"></i>
                    <h1 class="weather-value-forecast">{{day.weather_condition.maxtempF}}&deg;F</h1>
                    <h1 class="weather-value-forecast low">{{day.weather_condition.mintempF}}&deg;F</h1>
                </div>
            </template>
        </div>
    </template>
</template>

我处理窗口调整大小的事件,我根据视口的宽度更改“大小”聚合物属性。

rearrangeElements: function(){
        var polymer = this;
        var width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
        var height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

        //half size (icon)
        if(width <= 55){
            polymer.size = 'half';
        }

        if(width > 55 && width <= 168){
            polymer.size = 'regular';
        }

        if(width > 168 && width <= 300){
            polymer.size = 'double';
        }

        if(width > 300 && width <= 768 ){
            polymer.size = 'triple';
        }

        if(width > 768){
            polymer.size = 'quadro';
        }
    }

正确的模板在加载时呈现,一切都很好。但是,当 resize 事件触发并且变量发生变化时,什么也没有发生。

我认为当 size 属性发生变化时,基于我的模板绑定,另一个模板会被渲染,但我“大错特错”。我的问题是如何让聚合物根据更改 2 路绑定变量在模板之间切换?

非常感谢任何有关此的信息。

【问题讨论】:

    标签: javascript templates polymer shadow-dom 2-way-object-databinding


    【解决方案1】:

    所以我最终没有为不同的视图使用模板。相反,我将每个条件视图显示为一个 div,并将每个 div 上的 css 显示属性设置为一个聚合物变量(见下文)

        <div style="display:{{sizes.half}}">
            ...
        </div>
    
        <div style="display:{{sizes.regular}}">
            ...
        </div>
    
        <div style="display:{{sizes.double}}">
            ...
        </div>
    

    在我的脚本中,当我处理换屏事件时,我只是改变每个属性的值:

     polymer.sizes.half = 'none';
            polymer.sizes.regular = 'none';
            polymer.sizes.double = 'none';
            polymer.sizes.triple = 'none';
            polymer.sizes.quadro = 'none';
    
            //half size (icon)
            if(width <= 55){
                polymer.sizes.half = 'inherit';
            }
    
            if(width > 55 && width <= 168){
                polymer.sizes.regular = 'inherit';
            }
    
            if(width > 168 && width <= 300){
                polymer.sizes.double = 'inherit';
            }
    

    看起来很简单,(太简单了,闻起来很腥)但它解决了我的问题。希望这对可能遇到与我相同的问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多