So far we declared everything as a global function i.e., Person, PersonView, PersonCollection, etc. Namespacing helps us to structure our application in the much nicer way and keep the application with limited global variables.
Take a look on this.
In the above code, we specified App as a name space. You can define what ever you want to as per your project name. And in the App namespace, we specified some other name space i.e., Models, Collections, Views. You can define templates if you have multiple templates. But for this example, I keep template as a totally different global variable.
Understanding the namespacing
Cool. Now let’s take a look at our current naming conventions, We have
Implementation of namespace
So here is the new main.js file using namespacing in backbone js. (Please check inline comments..)
Now just refresh your page and you will see same collections views output but in much nicer way. ;) Thats all for Namespacing in backbone js. Stay tuned for next lesson ie. Jquery Dom events. On Click events etc. Super excited for that lesson. ;)
If you are facing any issues. Check out the source code files at github. I will be creating more and more directories in the same repo regarding each post. Still, if you have any doubts you can comment on the blog post itself, and I will try to reply back asap.