Single entry point redux
A couple months ago I posted about using a single entry point for JavaScript applications and how it could simplify the HTML logic and also make dependency management easier. A couple people asked if I had some example and the only examples I had were client projects which I couldn’t share the source code…
I’ve just answered a question on the RequireJS mailing list and used a Gist to explain how single entry point works and how it improves the code structure and simplify dependency management.
Here is a simplified example of how to have a single entry point for your app:
And how Crossroads.js can simplify the path resolution logic:
I hope this kind of pattern can help more people to build scalable applications. It certainly helps to avoid some hassles like working on files with 1000+ LOC or some error-prone concatenation scheme (my source files usually have less than 200 LOC). AMD isn’t just about lazy-loading/non-blocking scripts, for me the greatest benefits are being able to organize code across multiple files, be sure that all the dependencies are available before execution and also reduce the need of global variables and verbose namespaces. AMD FTW!
My code written with RequireJS is far more organized and awesome than any other #JavaScript I’ve written. – Ryan Florence
PS: use the RequireJS optimizer to combine all your source files for production, here is a basic example of how my build settings usually looks like.
Edit (2011/09/21): Added Crossroads.js example.