Scott Scott - 1 year ago 108
Javascript Question

Access polymer component properties from callback?

With this polymer element definition:

is: 'hello-world',
properties: {
dataId: {
type: String
value: {
type: String
ready: function() {
registerCallback(this.dataId, function (data) {
var z = data.someValue;
this.value = z;

When it tries to set
is actually the data, not the element. I want it to set the polymer property "value" to the value of z. How can I do this?

Here is the
if needed:

function registerCallback(id, callback) {
callbackMap[id] = callback;

Answer Source

There are two ways of doing it

  • As you already know this has a different meaning inside the callback so one way would be to store this(Polymer element) inside some other variable

    var self=this;
    registerCallback(function(){ = some value
  • Second option is to bind this with your callback

    registerCallback(function(){ some value
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download