Introduction
Welcome to the "Demonstrating Backbone.js" article series. This article demonstrates how to create and use collections in Backbone.js. This article starts with the concept of Backbone.js and various components of it. Previous articles have provided an introduction to views and the implementation of routers and collections. You can get them from the following:
- Demonstrating Backbone.js: Implement View
- Demonstrating Backbone.js: Implement View Part 1
- Demonstrating Backbone.js: Implement View Part 2
- Demonstrating Backbone.js: Implement View Part 3
- Demonstrating Backbone.js: Implement View Part 4
- Demonstrating Backbone.js :Implement View Part 5
- Demonstrating Backbone.js :Implement Routers Part 1
- Demonstrating Backbone.js :Implement Collections Part 2
- Demonstrating Backbone.js :Implement Collections Part 3
- Demonstrating Backbone.js :Implement Collections Part 4
In this article we will see how to use Namespaces.
Namespaces helps us to structure our application in a much nicer way and to keep the application with limited global variables.
See the inline comments for a better understanding.
- (function () { // entire block is wrapped into an anonymous jQuery function..
- window.App = { // defining app name space; we can rename it as per our project name.
- Models: {},
- Collections: {},
- Views: {}
- };
- window.template = function (id) {
- return _.template($('#' + id).html());
- };
- // Person Model
- App.Models.Person = Backbone.Model.extend({ // This is Person model referencing the App namespace model.
- defaults: {
- name: 'Guest User',
- age: 30,
- occupation: 'worker'
- }
- });
- // A List of People
- // Now here the People is referencing collection from App namespace
- App.Collections.People = Backbone.Collection.extend({
- model: App.Models.Person
- });
- // View for all people
- /// Now here People is referencing views from App namespace
- App.Views.People = Backbone.View.extend({
- tagName: 'ul',
- render: function () {
- this.collection.each(function (person) { // Change Person Reference from App Views namespace
- var personView = new App.Views.Person({ model: person });
- this.$el.append(personView.render().el);
- }, this);
- return this;
- }
- });
- // The View for a Person
- // Change Person Reference from App Views namespace
- App.Views.Person = Backbone.View.extend({
- tagName: 'li',
- template: template('personTemplate'),
- render: function () {
- this.$el.html(this.template(this.model.toJSON()));
- return this;
- }
- });
- // Change Person Reference from App Collections namespace
- var peopleCollection = new App.Collections.People([
- {
- name: 'Mahesh Chand',
- age: 26
- },
- {
- name: 'Praveen',
- age: 25,
- occupation: 'Dotnet Developer'
- },
- {
- name: 'Sam Hobbs',
- age: 26,
- occupation: 'Java Developer'
- }
- ]);
- // Change Person Views from App Views namespace
- var peopleView = new App.Views.People({ collection: peopleCollection });
- $(document.body).append(peopleView.render().el);
- })();
In this article, I explained how to use namespaces in Backbone.js. In a future articles we will understand more about Backbone.js with examples.

Join the conversation! Your thoughts help the community grow.