Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observers executed twice if defined in both the properties and the observers array #1884

Closed
josator opened this issue Jun 16, 2015 · 3 comments

Comments

@josator
Copy link

josator commented Jun 16, 2015

I reproduced this with this example:

http://jsbin.com/hoginazatu/1/edit?html,console,output

In this case the same observer is binded twice with the same variable, once in the properties and once in the observers array, resulting in the observer executed twice when there is only one change.

Also, I see a problem in this example:

http://jsbin.com/zepabasuge/1/edit?html,console,output

In this case two different variables are binded to the same observer, the two change, triggering two executions of the observer, the system should be intelligent enough to just call the observer once, after the update of both variables, like it happens in the following example:

http://jsbin.com/cihobeliju/1/edit?html,console,output

Is this correct or some setup would need this kind of behaviour?

@josator josator changed the title Observers executed twice if defined in the properties and observers array Observers executed twice if defined in both the properties and observers array Jun 16, 2015
@josator josator changed the title Observers executed twice if defined in both the properties and observers array Observers executed twice if defined in both the properties and the observers array Jun 16, 2015
@kevinpschaaf
Copy link
Member

All three are by design.

  1. Multiple observers for the same property will each be called as the property is set
  2. The same observer function for different properties will be called for each property as the property is set
  3. Only multiple property observers wait until all properties are defined before they are called

In this case two different variables are binded to the same observer, the two change, triggering two executions of the observer, the system should be intelligent enough to just call the observer once

This is the reason that multiple-property observers exist at all-- so you can disambiguate when you want the observer called for each individual property change and when you want the observer to require all properties defined. You must always declare the properties your observer depends on (access in the function), and so if your observer accesses more than one property, you should use a multi-property observer in the observers array.

@josator
Copy link
Author

josator commented Jun 16, 2015

Thank you for the clarification.

But in the example in 1 there are not multiple observers for the same property.
It is the same observer (fn) binded twice to the same property:

http://jsbin.com/hoginazatu/1/edit?html,console,output

Should It be allowed?

Well it is an special case in which the multiple-property observer only contains one property.

@pkgulati
Copy link

pkgulati commented Nov 2, 2016

If you want observer to be called only once, then why not define either in property or observer section, what is the need to define twice. Is there any special need to define twice. I am facing similar issue even though I have defined only in observer section, in case of route.path property of app-router.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants