2015-10-16 75 views
2

我有元素,我把它放在它的数据对象。从聚合物对象属性值设置属性

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

现在我没有这样做,所以我都在data财产申报。

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

我也必须点击应该切换布尔值的子元素的处理程序。

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

我需要我的元件上设置[active] attributedata.active值和更改每一次,因为我用它在我的CSS样式像:host[active] ...

我该如何实现它?我在事件处理程序中尝试this.setAttribute('active', this.data.active),但它只更改了一次值;控制台日志正在切换。

编辑:

我曾尝试使用此代码:

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); 
} 

但在我的控制台输出仅仅是这一点,所以观察者不叫。

------ click 
false 
------ click 
true 

回答

4

尝试在您的my-element中包含'active'作为属性并将reflectToAttribute设置为true。

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

编辑:请通过如何更改路径,以便通知观察员。您需要更新data.active如下

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

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

+0

我已经加入编辑我的问题。我之前尝试过这个解决方案,但“观察者”似乎不工作。它只能在页面加载时运行一次。 – HoBi

+0

@HoBi请检查我更新的答案。 – Srik