andresmijares
andresmijares

Reputation: 3744

Backbone Namespace convention best practice

This is a single question, but I have all my head about the fact if this is a good practice or not.

Basically, let's say we have this trivial scenario:

 (function(){

   window.App = {
    Models: {},
    Collections: {},
    Views: {}
   };

   App.Models.Person = Backbone.Model.extend({});
   App.Views.PersonView = Backbone.View.extend({});
   App.Collections.PeopleCollection = Backbone.Collection.extend({});


   var person = new App.Person(); 

 })();

So... if we are working on a small application, maybe it is ok, but when working on large size applications, it is ok to grab all the application into a self invoking anonimous function?, or what other practices do u guys point me out?

Upvotes: 2

Views: 346

Answers (2)

stakolee
stakolee

Reputation: 960

If you're building out a Backbone app of any non-trivial size, go with RequireJS. I'd recommend their Sugar Notation. The benefits to this approach:

  • Less risk of dependency issues. If the script is not defined, you won't be able to use it. With global objects you'll occasionally forget to define a script, but you won't notice it since a previous script already loaded it up. Then 1 out of every 25 times your page will fail because the calling order gets jumbled. This the worst error imaginable, and you can completely avoid it.
  • No need to pollute the global scope with your instance variables
  • Cleaner to require's ideals

What this means is you save the namespacing for your directory structure. How you define the variables representing the classes is more of a "gentleman's agreement".

Upvotes: 2

Akos K
Akos K

Reputation: 7133

We decided to use RequireJS to organize our huge codebase. It turned out great.

It encourages modular and clean structure. We ended up without having a single window.Whatever globally visible stateful troublemaker, and we like it.

If you're new to Backbone/RequireJS I recommend you the following tutorial:

http://backbonetutorials.com/organizing-backbone-using-modules/

Upvotes: 2

Related Questions