javascript - Setting attribute on element gives error "Cannot set property '...' of undefined' with Polymer -


i'm trying set custom element using polymer consists of draggable box icon in it. i'd able define icon specifying icon attribute on element, can declarative create elements rather defining seticon method on element, accessible through javascript.

i've tried set element handle attribute including properties attribute on element prototype i'm feeding polymer, giving me element code looks this:

var elementproto = {     : "my-element",     behaviors : [someotherproto],     properties : {         icon : {             "type" : "string",             "observer" : "_iconobserver",         }     },     _iconobserver : function(){ /* functionality adding icon */} };  elementproto.othermethods = various functions;  polymer(elementproto); 

in file, i'm trying set icon attribute using setattribute in javascript function, (after importing new element via associated html file) so:

var newelement = document.createelement("my-element"); newelement.setattribute("icon", "some/icon/path.png"); 

ideally, fire observer function new path argument , set url image in new element whatever path was. instead, chrome gives me error:

uncaught typeerror: cannot set property 'icon' of undefined 

why getting error, , can fix it?

notes on debugging:

i've dug polymer code , found error originates line:

var model = this._clientsreadied ? : this._config; 

both this._clientsreadied , this._config undefined, causing error in ._setattributetoproperty(model, name); method, name set attribute on model. don't know enough polymer know why attributes undefined, though.

why getting error, , can fix it?

you getting error because @ time invoke createelement(), custom element not registered yet. because 2 files parsed in wrong order.

to fix issue, can try change order of imported files, or can wait htmlimportsloaded, or webcomponentsready event.

document.addeventlistener("htmlimportsloaded", function ()  {     var newelement = document.createelement("my-element");     newelement.setattribute("icon", "some/icon/path.png"); }); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -