Refactor js

**Resolves issues #777 and #780.**

---

Rewriting our js to be more readable and less hacky.



YMMV, I guess, but to me it's just visual noise. I don't think the word "when" makes it any more clear that `.done()` is going to add a function that will called when the Deferred is resolved.