Working with asynchronous code is one of the best features of JavaScript. With Ajax, it's very simple to request some JSON API, add a callback and update the UI with the result of that request very easily. Especially when using jQuery.
However, writing an application that is testable, decoupled, and manageable, is a whole other beast. The typical approach to making Ajax requests in an application is to use jQuery's Ajax, pass a success callback and update the DOM with the response. That's great if you have some small application that doesn't need a lot of updates and manageability.
What happens in an enterprise environment when someone writes that kind of shortcut Ajax code, makes a mess of the DOM in the success callback? You've just coupled your DOM directly to your request. What if the new boss comes in and says, "I HATE TEH JQUERY!"? Well, at that point you have to find the code in the success callback that messed with the DOM, rip it out, rewrite your Ajax logic, etc... It's a mess.
A great way to solve this problem is through messaging. A great open-source JavaScript messaging library is postal. Take a look at how 3 objects having separate responsibilities can use postal and not know that each other exist, thereby decoupling the application, and making it testable as well!
Your new post is loading...