问题描述:

I have a child custom element with data property.

My goal is to assign an event listener to this property (using javascript and not {{}} data-bind syntax) in the context of the parent element.

I search something like this:

ready(){

this.addEventListener(this.$.childElememt.data, _onChildDataChange);

},

_onChildDataChange() {

//called when data property of child elememt changes

}

网友答案:

In Polymer, properties that have notify: true set on them will cause the element to fire a non-bubbling <property>-changed event that their parent can listen to.

I suspect you could listen to this event manually (provided that the child property is set up to notify) by doing something like this:

ready() {
    this.$.childElement.addEventListener('data-changed', this._onChildDataChange.bind(this));
},

_onChildDataChange() {   
   //called when data property of child element changes     
}

However, this is effectively equivalent to just using the two-way data-binding syntax on the child with an observer on the parent property, so I would recommend that you do that instead if possible. The data-binding is basically already doing the exact same thing for you.

相关阅读:
Top