I have several different JavaScript objects with methods that i want to keep separate. However, I need some type of observer, callback, or plugin design so that I can trigger these methods at the correct time.
For example, A.Foo() should almost always run after B.Bar() - but I do not want to place a call to A.Foo() inside B.Bar() because there are those odd times it should not run after B.Bar(). That would be coupling my code and I know that's bad.
I want to approach this problem as if B.Bar() is just doing it's job and does not ever plan on knowing about A.Foo() or any other function that might want to tag along with it. In other words, plugin support for future developers.
How do you design modular, non-coupled code with observer or event based callbacks in Javascript?
A.Foo()to true and haveB.Bar()immediately return if the global variable is false?B.Bar(). Otherwise it depends on what you're actually doing, you could also use some sort of deferred-style mechanism so you don't need to pass it in.