Using Event with Polymer

Categories: HTML5; Tagged with: ; @ October 15th, 2015 22:26

Dispatching Event:

<link rel="import" href="../bower_components/polymer/polymer.html" />
<link rel="import" href="../bower_components/iron-icons/iron-icons.html" >
<link rel="import" href="../bower_components/paper-button/paper-button.html" />

<dom-module id="item-detail">
    <template>
        <div id="container">
            <h3>{{item_id}}</h3>
        </div>
        <paper-button on-tap="onAddToCart"><iron-icon icon="icons:shopping-cart"></iron-icon>AddToCart</paper-button>
    </template>

    <script>
        Polymer({
            is: 'item-detail',

            properties: {
                item_id: {
                    type:Number,
                    value:-1
                }
            },
            onAddToCart: function(e) {
                console.log('adding...');
                this.fire('eventAddToCart', {item_id: this.item_id});
            }
        });
    </script>
</dom-module>

Event Listener:

<link rel="import" href="bower_components/polymer/polymer.html" />
<link rel="import" href="element/item-detail.html">

<dom-module id="main-app">
    <template>
        <item-detail id="item_detail" item_id="{{current_detail_id}}"></
    </template>
    <script>
        Polymer({
            is: 'main-app',

            listeners: {
                'item_detail.eventAddToCart': 'onAddToCart'
            },

            onAddToCart: function(e) {
                console.log('Event received' + e.detail.item_id);
                // this.addToCart(e.detail.item_id);
            }
        });
    </script>
</dom-module>

Polymer helloworld: create web component using dom-module

Categories: Development NotesHTML5; Tagged with: ; @ July 19th, 2015 23:10

Requirement:  creating a component to display user info

Step 1:  Create a web component using dom-module:

user-info.html:

<link rel="import" href="../bower_components/polymer/polymer.html" />

<dom-module id="user-info">
    <template>
        <div id="container">
            Hi, <span>{{user_name}}</span>!
        </div>
    </template>

    <style>
        #container {
            font-size: 11px;
            color: gray;
        }

    </style>

    <script>
        Polymer({
            is: 'user-info',
            properties: {
                user_name: {
                    type:String,
                    value:"Mr.default"
                }
            }
        });
    </script>

</dom-module>

Step 2: Use the newly created component:

<link rel="import" href="components/user-info.html" />
<template is="dom-bind">
    <user-info user_name="Li"></user-info>
</template>



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.