Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set attribute from Polymer object property value

I have element and I put in it object with data for it.

<my-element data="{{item.content}}"></my-element>

For now I have not done this so I have all in data property declaration.

properties: {
  data: {
    type: Object,
    value: {
      active: false,
      name: "Some name."
      ...
   }
  }
}

I also have to click handler on child element which should toggle the boolean value.

_handleClickBlock: function () {
  this.data.active = !this.data.active;
}

I need to set [active] attribute on my-element to data.active value and make changes every time, because I use it in my CSS styles like :host[active] ....

How can I achieve it? I tried this.setAttribute('active', this.data.active) in event handler, but it change the value only once; console log was toggling.

Edit:

I have tried use this code:

observers: [
    '_activeChange(data.active)'
],

_activeChange: function(newValue) {
    this.active = newValue;
    console.log("change");
},

_handleClickBlock: function () {
    console.log("------ click");
    this.data.active = !this.data.active;
    console.log(this.data.active);
}

But in my console output is only this, so observer is not called.

------ click
false
------ click
true
like image 791
tenhobi Avatar asked Sep 01 '26 22:09

tenhobi


1 Answers

Try including 'active' as a property in you my-element and set reflectToAttribute to true.

properties: {
  data:{...}
  active: {
    type: Boolean,
    reflectToAttribute: true
  }
}
observers: [
  '_activeChange(data.active)'
],
_activeChange: function(newValue) {
  this.active = newValue;
}

Edit: Please go through how to change the path such that observers are notified. You need to update data.active as below

this.set('data.active', !this.data.active);

Below jsbin: http://jsbin.com/qupoja/4/edit?html,output

like image 52
Srik Avatar answered Sep 04 '26 14:09

Srik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!