PatternsForLoadingDependencies

Situation

Q: How does this format look, Peter? A: I like it!

So you come across this situation, and want to do X. How can you do it?

Solution

Pattern 1

Pros

Cons

Pattern 2

Pros

Cons


Problem: 'this' gets redefined in callbacks from external function calls (esp. Ajax)

Solution:

Either use Peter Svensson's homebrew jda util library , which incorporates a small piece of prototype 1.4, renamed to mybind, or use dojo's hitch function, which does the same.

Pattern 1

ajax.get(url, this.newbpCallback.mybind(this));

Pros

Your code doesn't break. Your 'this' object is the same as outside/before the function call

Cons

You add external dependencies. Of course you can easily just copy the two functions in Peter's util.js in your own code, it's maybe fifteen lines of code, if that.

Pattern 2

Pros

Cons