Recently when faced with taking on a codebase that had grown unmanageable due to the procedural model that had been employed thus far, I began by refactoring the code around a central event dispatcher, allowing many of the large methods — that had previously been created — to be broken down into smaller more loosely coupled pieces.
By introducing an asynchronous model to the code, we were able to break much of it into smaller more maintainable pieces, including a clean break between presentation — such as animations or manipulating the DOM — and the business logic, involving AJAX requests and API calls.
A more loosely coupled system also helped to simplify the development, allowing smaller related sections to be developed independently and by different members of the team.
The same technique could be created using jQuery custom events on the document element, although it would be advisable to use one of the dedicated plugins available such as jQuery pubsub by Peter Higgins2.
There are also library agnostic implementations such as the brilliant PubSubJS3 by Morgan Roderick — whom I was fortunate enough to speak with at the fullfrontal4 pre conference drinks earlier this month — and after reading through the code on github I can highly recommend his library.
© 2010-2012 Daniel Knell