javascript - 通过 2 路绑定(bind)在嵌套模板之间切换 polymer

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

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

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

我处理一个事件,在窗口调整大小时,我根据视口(viewport)的宽度更改“大小” polymer 属性。

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';
        }
    }

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

我认为当大小属性发生变化时,根据我的模板绑定(bind),另一个模板将被渲染,但我错了。我的问题是如何让 polymer 根据更改 2 路绑定(bind)变量在模板之间切换?

如果有任何关于此的信息,我们将不胜感激。

最佳答案

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

    <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';
        }

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

关于javascript - 通过 2 路绑定(bind)在嵌套模板之间切换 polymer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258716/

相关文章:

javascript - 在 HTML/HTML5 中使用 JS 的不规则 anchor 和形状

javascript - 来自 javascript 的 polymer 数据绑定(bind)

javascript - Polymer-Project 对象未在 OS X 10.10 中呈现

javascript - AngularJS 中的链接选项

javascript - 将值从 View 传递到动态填充表单的 Controller

c++ - 具有非空模板参数列表的模板特化

c++ - 如何将模板化类实例作为模板参数传递给另一个模板?

c++ - 如何在编译时获得多维 std::vector 的深度?

javascript - polymer 铁列表迭代,但不打印

javascript - tr 元素上的 ui-sref 不会在新选项卡中打开