Is it possible ( or a way ) to pass parameters for CustomElement constructor?

590 views
Skip to first unread message

kara...@gmail.com

unread,
Aug 11, 2014, 11:03:39 PM8/11/14
to polym...@googlegroups.com
Hi,

I'm using polymer in my project and it works well. Until recently I'm start create my CustomElement dynamically. Basically I define a template and a constructor so that I can create my CustomElement by constructor in script. 

I notice when I call the the constructor, the created and ready event will be triggered immediately. That makes me hard to adjust settings for CustomElement.

For example, I have the following template:

<polymer-element
   
name="fire-ui-unit-input"
   
constructor="FireUnitInput"
   
>
<template>
<link rel="stylesheet" href="unit-input.css">


<input id="input"
   
class="input"
   
type="text"
   
placeholder="-"
   
></input>
<span class="unit">{{unit}}</span>
</template>

<script>
    Polymer('fire-ui-unit-input', {
        publish: {
            value: null,
            unit: '',
            type: 'int', // int, float
            precision: 2,
            interval: null,
            min: null,
            max: null,
            focused: {
                value: false,
                reflect: true
            },
        },

        ready: function() {
            switch ( this.type ) {
                case 'int': 
                    this._min = (this.min!==null) ? parseInt(this.min) : Number.NEGATIVE_INFINITY;
                    this._max = (this.max!==null) ? parseInt(this.max) : Number.POSITIVE_INFINITY;
                    this._interval = (this.interval!==null) ? this.interval : 1;
                    break;
            }
        },
    }

</script>
</polymer-element>

And In some case I have to dynamically create this CustomElement like this:

var unitInput = FireUnitInput();
unitinput
.unit = 'cm';
unitinput
.type = 'float';

Since the constructor of my CustomElement immediately call ready(), I don't have a chance to pass unit and type attributes to it. In the code above, I have to manually call unitInput.ready() again or some other function to do the init stuff. 

My question is: is there a way to let the CustomElement's constructor receive attributes from code?

Rob Dodson

unread,
Aug 11, 2014, 11:49:40 PM8/11/14
to kara...@gmail.com, polymer-dev
To my knowledge there's no way to pass arguments to the constructor. But it brings up the bigger question of how do you kickoff an element when it requires more than one attribute to be set. geelen, who created x-gif, and some of the folks from Mozilla were discussing it recently: https://gist.github.com/geelen/2e314d9c547b752603fd


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/5e554576-0ddb-45b2-9f4d-3e38d8f4e326%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages