javascriptbackbone.jsrequires

How to implement Nested namespacing in Backbone.js


I have been reading some articles and video tutorials to get started with Backbone.js. Currently I'm reading Addy Osmani's Backbone Fundamentals. This section on namespacing suggests YUI-like nested namespacing but as I'm new to BB, I want some help with code.

My actual question is how to implement nested namespacing in backbone with requirejs (with an example please).

Sincere regards,


Solution

  • This is a pretty basic example from my app.js, which is loaded after the requirejs.config in main.js via require(['app']). I create my namespace here, by simply adding an object to the window (and, ok, it does not check for existance, but it is a one-page-app and therefore loaded only once). From here, window.App is available globally, from all controllers and views.

    define([
            'config',
            'jquery',
            'backbone',
            'router',
            'i18n!nls/strings'
        ],
        function(config, $, Backbone, Router, i18n) {
            'use strict';
    
            // Create App namespace
            window.App = {};
    
            // Set translations
            window.App.i18n = i18n;
    
            // Assign the router
            window.App.router = new Router();
        }
    );