Polymer array as member in element

Cédric

I'm trying to acces a variable in an array as a member of a polymer-element. But I keep getting "Cannot read property NaN of undefined". This is my code, I get the error at this.images[this.x]:

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

<polymer-element name="big-picture" noscript>

<template>
    <style>
    #crop
    {
        display: block;
        position: relative;
    }
    #mainpictop
    {
        position: absolute;
        width: 100%;
    }
    #mainpicbottom
    {
        position: absolute;
        width: 100%;
    }
    </style>

    <div id="crop">
        <img id="mainpictop" src={{pic1source}} style="opacity:{{pic1opacity}}"></img>
        <img id="mainpicbottom" src={{pic2source}} style="opacity:{{pic2opacity}}"></img>
    </div>


</template>
<script>
    Polymer('big-picture',{
        x:1,
        currentTop:0,
        currentBottom:1,
        numpics:5,
        transitionspeed:200,
        images: new Array,
        pic1opacity: 1,
        pic2opacity:1,
        pic1source: "img/main/1.jpg",
        pic2source: "img/main/2.jpg",
        ready: function(){


            for (i=0;i<this.numpics;i++)
            {
                this.images[i]="img/main/".concat(i+1).concat(".jpg");
            }

            setInterval(function() {
                changeImageSrc()
          },this.transitionspeed);
        }
    });
    function changeImageSrc() {

    if(this.pic1opacity==1)
    {
        this.pic1opacity=0;
        this.pic2opacity=1;
        if (this.currentTop!=this.currentBottom)  
            this.pic2source = this.images[this.x];
        else
        {
            this.x=this.x+1;
            if(this.x===this.numpics)
                this.x=0;
            this.pic2source = this.images[this.x];
        }
        this.currentBottom=this.x;
    }
    else
    {
        this.pic2opacity=0;
        this.pic1opacity=1;
        if (this.currentTop!=this.currentBottom)  
            this.pic1source = this.images[this.x];
        else
        {
            this.x=this.x+1;
            if(this.x===this.numpics)
                this.x=0;
            this.pic1source = this.images[this.x];
        }
        this.currentTop=this.x;

    }



  this.x=this.x+1;
  if(this.x===this.numpics)
    this.x=0;
}

</script>
</polymer-element>

I've only just began to learn Polymer so don't be too harsh please.

adam8810

This is a scope issue. Be very careful when using "this" within other functions because the context of "this" changes to the function it is in. In your case the "this" variable has been set to changeImageSrc()'s context.

An easy fix is to store a reference to "this" in another variable (var _this = this;) and use that new variable when you need to take actions upon your Polymer variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select first element in array in Polymer

From Dev

Binding sub property of array element in Polymer

From Dev

Checking if a modified element of an array is a member in the same array

From Dev

array of class element as a static constexpr member

From Dev

How to specify member pointer to an array element?

From Dev

new element is Array overwrites previous member swift

From Dev

clone of polymer element remove template of polymer element

From Dev

Polymer - Show element inside polymer element

From Dev

Trying to append a Polymer element in another Polymer element

From Dev

Polymer querySelector can't find custom polymer element in polymer element

From Dev

Polymer querySelector can't find custom polymer element in polymer element

From Dev

"Ambiguous reference to member map" when attempting to append/replace array element

From Dev

How to access a property of an object (stdClass Object) member/element of an array?

From Dev

Initialization of an array of structure initializes all member of a single element, why?

From Dev

Get polymer parent element

From Dev

Selecting element (Polymer 1.0)

From Dev

Polymer property not in element

From Dev

Passing data to a Polymer element

From Dev

YUIDocs parse Polymer element

From Dev

Polymer - Updating Element

From Dev

Add a class to polymer element

From Dev

Polymer element blur event

From Dev

Dynamically create polymer element

From Dev

Polymer styling element

From Dev

Polymer element with javascript dependencies

From Dev

Accessing a nested polymer element

From Dev

Polymer element not showing in firefox

From Dev

Contenteditable inside Polymer element

From Dev

Polymer defining properties in element

Related Related

HotTag

Archive