Edw
Edw

Reputation: 31

How to call custom method of a element in Polymer 1.0?

I'm coding a new version of a chrome extision, but got a trouble.

Here's my element code: Element code

At line 32 I'm tring to call the custom method willUpdateBackground, console tells me the method is undefined. Code below.

Polymer({
  is: 'pure-newtab',
  ready: function() {
    console.dir(this.$.background);
    this.$.background.willUpdateBackground('color', '#333');
    // This line get error, 'willUpdateBackground' is undefined
  }
});

The definition

Polymer({
  is: 'pure-background',
  ready: function () {
  },
  willUpdateBackground: function(type, imageOrColor) {
    console.log(type);
    console.dir(this);
  }
});

But if I store this object, in console I can call the temp1.willUpdateBackground, it's weried. Screenshot: https://i.sstatic.net/OTLgw.png

Fixed And Problem Happens Again I move the custom method call to attached callback, elements not ready in ready function in some environment. And after I fixed it, it happens again: in attached method, call failed again, and I move it to async call with 100ms delay, problem disappear again. It's not a solid solution, I'm still finding a way out.

    this.async(function () {
      this.$['background'].updateBackground('rgba(255,128,0,1)', 'url(https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png)');
    }, 100);

Upvotes: 1

Views: 599

Answers (1)

Alan Dávalos
Alan Dávalos

Reputation: 2698

I tried pasting your code into this jsbin and it worked just fine... (I commented your console.dir() and added a couple of console.log() of my own, but that's all I did) http://jsbin.com/tomewigobu/1/edit?html,console,output

I would suggest you check that your polymer version is updated, that should help.

Upvotes: 1

Related Questions