garyx
garyx

Reputation: 619

What's the differences between observers and listeners in polymer?

If I set the property as follow:

properties: {
    myProp: {
        type: Object,
        notify: true,
        observer: '_onChangedByObserver',
    }
},
listeners: {
    'my-prop-changed': '_onChangedByListener'
}

When I run the code, both _onChangedByObserver and _onChangedByListener are called whenever myProp changed.

So how to determine which to used?

ps: I want to tell whether the change comes from external consumer or internal assignment. What should I do?

Thanks!!

Upvotes: 3

Views: 1561

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657058

Observers are called when the value of the property is changed. Listeneres are event handler for all kind of events, not only property changes. For observers to work you don't need to set notify: true, if you want a listener to be called on property change, this is necessary. Also the method signature differs. Observers get passed the new and the old value while listeners get an event with the new value in the details field.

I don't think there is a way to tell what caused the change in both cases.

Upvotes: 6

Related Questions