Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write condition in polymer1.0 with "dom-if"?

I have below code:

<template is="dom-if" if="{{item.hasAttach}}">
     <i class="fa fa-paperclip"></i>
</template>

item.hasAttach = true/false

But I want to check condition in this if like : item.content_format_code == 'PDF'

<template is="dom-if" if="{{item.content_format_code == 'PDF'}}">
         <i class="fa fa-pdf"></i>
    </template>
<template is="dom-if" if="{{item.content_format_code == 'JPEG'}}">
         <i class="fa fa-jpg"></i>
    </template>
<template is="dom-if" if="{{item.content_format_code == 'xls'}}">
         <i class="fa fa-xls"></i>
    </template>

it should be like {{item.content_format_code == 'PDF'}} = true/false But it is not testing this. I want to show icon as per file type. item.content_format_code == 'PDF' this is not checked true/false. In polymer it takes only true/false as a conditional actual value but don't check expression. Please Help me.

like image 750
Ravi Avatar asked Aug 18 '15 10:08

Ravi


People also ask

How do you use DOM in polymer?

The <dom-if> element will stamp a light-dom <template> child when the if property becomes truthy, and the template can use Polymer data-binding and declarative event features when used in the context of a Polymer element's template. When if becomes falsy, the stamped content is hidden but not removed from dom.

Which helper element will you use to improve the memory footprint of large and coplex sites?

Conditional templates introduce some overhead, so they shouldn't be used for small UI elements that could be easily shown and hidden using CSS. Instead, use conditional templates to improve loading time or reduce your page's memory footprint.


2 Answers

You can use computed bindings.

Define a function that computes the expression and bind it to the dom-if.

<template is="dom-if" if="[[isFormat(item.content_format_code, 'PDF')]]">
     <i class="fa fa-pdf"></i>
</template>

Polymer({
    is: "my-element",
    isFormat: function(code, format) {
        return code === format;
    }
});
like image 97
Maria Avatar answered Sep 18 '22 23:09

Maria


Currently, polymer only supports simple constructs for conditions. This means that you can't write something like

[[ item.something == 'CONDITION' ]]

You are left with 2 choices:

  1. The item used for the condition is a boolean, than simply writing

    [[ item ]]
    

    or

    [[ !item ]]
    

    will work. The only operator you can use is '!'

  2. With more complex conditions, use computed bindings:

    [[ _computeResult(item) ]]
    
like image 34
urandom Avatar answered Sep 19 '22 23:09

urandom