I have an object Foo
that has 2 public properties: A
and B
.
These properties can be changed from outside, as the application I'm working on is using a data-binding architecture. This is the front-end (client) code, but it's also communicating with a server to fetch data.
So my object is listening for changes to both A
and B
.
Any change in A or B is triggering the update()
method, which triggers a request to the server.
I also have a piece of logic in my object Foo
that says every time you change B
, you have to reset A
to its initial value.
My problem is, how do I deal with updating B without triggering the update()
method twice?
Looking at the image above, the green path should always trigger an update. The red path ends up sometimes triggering 2 updates, because by changing B it changes A (if it's not the initial value), which always triggers an update.
I have to add that everything is asynchronous so the observers for my properties don't notify me immediately after changing objects. (design choice of the platform and language I'm working in – Polymer.JS). So this makes it harder to set a flag before changing B
from A
- I tried this but it didn't work, as the watcher method gets triggered after the chain of changes has executed.
As I come from the front-end world, with a background in graphic design, I don't have the knowledge necessary to spot the mistakes I might be doing, but I want to learn, so I'd like you to point me to reference material and/or books I can read about the correct applications of the observer pattern.
Is there a pattern that deals with this particular scenario and what are possible solutions to this?
If the observer pattern isn't the right choice for this, is there another pattern I could use internally that allows me to expose only the two properties A
and B
to the outside world?
A third option would be that I'm applying the right pattern, but the platform doesn't support what I'm trying to achieve. I'd like to know this so I can post the question on StackOverflow under the right tags.