Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:16:14+00:00 2026-06-11T00:16:14+00:00

I am working on a big Backbone.js application. The code is modular structured using

  • 0

I am working on a big Backbone.js application. The code is modular structured using require.js.
Now I see a lot of backbone code and tutorials doing this:

window.app = ( window.app || {} );

after they will assign model definitions and Collection instances to that global object, like so:

Task = Backbone.Model.extend({ /*...*/ });
Tasks = Backbone.Collection.extend({ /*...*/ }); 

window.app.Task = Task;
window.app.Tasks = new Tasks();
// do this with all your models and collections

I like this approach for its simplicity and for not having to deal with where and when to instantiate a collection. But it somehow seems wrong to first separate the code into tiny bits using require.js and after assign all together to a global variable (apart from that global variables are generally bad codestyle in javascript).

So what is your take on this, what are the pros and cons of this approach and how do you deal with your objects in Backbone?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:16:16+00:00Added an answer on June 11, 2026 at 12:16 am

    First of all its not that big a problem to pollute your global name space as you can wrap all your model in your own namespace:

    app = (app || {} );
    app.Task = Backbone.Model.extend({ /*...*/ });
    app.Tasks = Backbone.Collection.extend({ Model:app.Zask }); 
    

    When it comes to requirejs it depends on the size of your project. I think for smaller projects you can go without. The benefit of using requirejs isn’t the preventing of namespace pollution, its the better maintainability because you always see the dependencies of a module. You can write better unit tests, because you can test a module and mock out all its dependencies. So we rebuild a large Backbone app from a structure like the above example into something like this:

    models/Task.js

    define(function () {
      return Backbone.Model.extend({ /*...*/ });
    });
    

    collections/Tasks.js

    define([models/Task], function (Task) {
      return Backbone.Collection.extend({ Model:app.Task }); 
    });
    

    app.js

    define([collections/Tasks], function (Tasks) {
      var tasks = new Tasks();
      tasks.fetch();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

see i am working in one BIG project source code Now i want to
I am working on big application build using ASP.NET which was started 1.5 years
i'm working in a big and rich rails web application using tons of javascript.
I'm working on a big project so I use code folding a lot. I
I've just started using Backbone (and Underscore) JS. We are doing a big iPad
i have this code (im working with big files support in ansi c) unsigned
I'm working on a big c++ project and change code from other people. During
I'm working on a big Qt application with multiple widgets which are quite complex.
I'm currently working on a big web application for a company and we are
I'm working on this big website and I want to put it online before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.