Organize jQuery code without a framework
When I look for ways to organize my jQuery code on the internet, what I find are usually advices on what framework I should use. Using a framework is a good idea because good frameworks enforce good design practices. But the core of any framework is just a set of rules, so if you want to keep things really simple, you can adopt some of those rules to write good JavaScript without any additional libraries. This is particularly useful when you just need to add some dynamic functionality to a html page and using something like Backbone or Angular seems an overkill. I use this technique for small projects or when prototyping interfaces.
Here are two rules I adopted from frameworks for my jQuery code:
- Write isolated pieces of code that only control a specific element on the page. I will call these pieces controllers for convenience.
- Define behavior of a controller only by attaching event...