How do I use namespaces in Backbone with RequireJs

Posted by dev.pus on Stack Overflow See other posts from Stack Overflow or by dev.pus
Published on 2012-07-08T09:12:25Z Indexed on 2012/07/08 9:15 UTC
Read the original article Hit count: 349

Filed under:
|
|

I am unsure how I use namespaces in an modularized (RequireJs) Backbone environment.

I have thought a bit how it could look like but am totally unsure if this is the right way.

app.js (getting executed by main.js)

define('App', ['underscore', 'backbone', 'Router'], function( _, Backbone, Router){
    function initialize(){
        var app = {}; // app is the global namespace variable, every module exists in app

        app.router = new Router(); // router gets registered

        Backbone.history.start();
    }

    return { initialize: initialize }
});

messages.js

define('MessageModel', ['underscore', 'backbone', 'App'], function(_, Backbone, App){
    App.Message.Model; // registering the Message namespace with the Model class

    App.Message.Model = Backbone.Model.extend({
        // the backbone stuff
    });

    return App;
});

Is this the right approach or am I fully on the wrong way (if yes please correct me!)

© Stack Overflow or respective owner

Related posts about backbone.js

Related posts about namespaces