polymer - polymer 条件模板的限制?

标签 polymer

我正在基于 JSON 对象中可用的数据开发一组条件 View - 实际上,如果我们有媒体要显示,则显示媒体 View ;如果我们只有文本信息要显示,则显示文档 View ,等等迄今为止我一直使用的方法是使用 hasOwnProperty() 检查 JSON 对象以确定可用数据并根据其中的内容制定 View 模板。

我已经实现了一些作为准系统版本的东西,但现在我什么也没得到。 if 似乎只是杀死了嵌套模板。这是我正在尝试的:

        <template bind if="{{ posts[postid].hasOwnProperty('video') }}">

            <div class="tileHeader">Posted by @{{ posts[postid].creator_id }} <time-ago datetime="{{ posts[postid].creation_date }}"></time-ago></div>
            <div class="tile">
                <div class="heroTop" style="background-image: url({{ posts[postid].body }}) no-repeat"></div>
                <div class="heroBottom">
                    <div class="headline">{{ posts[postid].url_title }}</div>
                    <div class="postDesc">{{ posts[postid].url_description }}</div>
                </div>
                <div class="attribution">
                    {{ posts[postid].url }}
                </div>
            </div>

        </template>



        <template bind if="{{ posts[postid].hasOwnProperty('image') }}">

            <div class="tileHeader">Posted by @{{ posts[postid].creator_id }} <time-ago datetime="{{ posts[postid].creation_date }}"></time-ago></div>
            <div class="tile solo-view">
                <div class="heroSolo">
                    {{ posts[postid].body }}
                </div>
                <div class="attribution">
                    {{ posts[postid].url }}
                </div>
            </div>

        </template>

两个问题: 1. 这个 if 语句可以在这种情况下工作吗?或者是否需要将其重新构建为过滤器? 2. 对于给定的渲染,如果两个 if 都为 true,会发生什么情况?

最佳答案

好的,这似乎有效。很乱吗?当然是。 实际上,从我的 API 中,我得到了大量的 post_ids,我需要根据我发现的内容对其进行不同的格式化。尝试使用 JSON.hasOwnProperty 之类的东西不起作用(不知道为什么),所以我求助于基于单独的发现函数分配变量。

有更好的方法吗?对此,我确信。如果您有更好的方法,请告诉我。但这就是我的结论:

<template repeat="{{ postid in postids }}">
        <core-ajax id="postdetail" url="api/1/posts/{{ postid }}" data-postid="{{ postid }}" postid="{{ postid }}" handleAs="json" method="GET" auto on-core-response="{{ updatePostDetail }}"></core-ajax>

        <template if="{{ posts[postid].displaytype == 'articleImage' }}">

            <div class="tileHeader"><user-print creatorid="{{ posts[postid].creator_id }}" prepend="Posted by" size="small"></user-print> <span hidden?="{{ showchannel }}">In channel {{ posts[postid].channel_id }}</span> <time-ago prepend="Last update " isostring="{{ posts[postid].creation_date }}"></time-ago></div>
            <div class="tile media-view" style="background: url({{ posts[postid].banner }}) no-repeat; background-size: cover;" title="{{ posts[postid] | descText }}">
                <div class="heroBottom">
                    <div class="type">{{ posts[postid].displaytype }}</div>
                    <div class="headline">{{ posts[postid].url_title }}</div>
                    <div class="postDesc">{{ posts[postid].body | stripTags | shorten }}</div>
                    <div class="attribution"> {{ posts[postid].url }} </div>
                </div>
            </div>

        </template>

        <template if="{{ posts[postid].displaytype == 'video' }}">

          ... (etc)

        </template>

</template>

<script>

Polymer('post-list', {
    postids: [],
    posts: {},
    created: function(){

    },
    ready: function(){

    },
    updatePostList: function(e){
        this.postids = e.detail.response.post_ids;
    },
    updatePostDetail: function(e){
        json = e.detail.response.post;
        postid = json.id;
        this.posts[postid] = json;

        this.posts[postid].displaytype = 'barePost'; // default value so I don't have to worry about a bunch of similar 'else' statements
        this.posts[postid].hasVideo = 'noVideo'; // ditto

        if(json.hasOwnProperty('url_meta_tags')){
            if(json.url_meta_tags.hasOwnProperty('og:video') || json.url_meta_tags.hasOwnProperty('twitter:player:stream')){
                this.posts[postid].hasVideo = 'video';
                this.posts[postid].displaytype = 'video';
            }
            else if(json.url_meta_tags.hasOwnProperty('og:image') || json.url_meta_tags.hasOwnProperty('image') || json.hasOwnProperty('banner')){
                if(json.body.length > 350){
                    this.posts[postid].displaytype = 'longArticle';
                }
                else if(json.body.length > 0){
                    this.posts[postid].displaytype = 'articleImage';
                }
                else{
                    this.posts[postid].displaytype = 'bareImage';
                }
            }
        }
        else if(json.hasOwnProperty('files')){
            this.posts[postid].displaytype = 'embeddedMedia';
        }
    }

    </script>

关于polymer - polymer 条件模板的限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924203/

相关文章:

html - polymer 1.0 中的砌体

javascript - :target css selector not working in polymer

google-maps - Google map 标记不适用于 Safari 中的 Shadow DOM

javascript - 在 Polymer 1.0.0 中让子组件调用他的父 API

javascript - 在 Polymer 元素中调用函数

javascript - 如何让polymer.js 软件报告错误/异常?

json - 在 polymer 中延迟加载巨大的 json 数据

javascript - 使用 Polymer 执行 javascript/Angular 部分

javascript - 我如何使用 polymer 纸卷轴标题面板进行无限滚动?

javascript - 在 Polymer 中铁形式的 ajax 响应后更新 UI