Namespacing in backbone js - Learning backbonejs
This entry is part 11 of 14 in the series for A Complete Guide for Learning Backbone Js
- Introduction and Installation
- Representing your data in javascript
- Defining Models in backbone js
- Adding Validations in models backbone js
- Explaining views in backbone js
- How to use templates in backbone js
- How to improve templates in backbone js
- Collections in backbone js
- Collection views in backbone js
- Template helpers in backbone js
- How to use namespace in backbone js
- How to handle dom events in backbone js and define your custom events (Live Demo)
- Routing in backbone js
Agenda
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.
Quick Look
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. ;)
Source code
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.