Search code examples
javascriptjquerybackbone.jsmarionette

Backbone Namespaces Open View


I hope someone could give me some usefull hints.

app.js

;(function(app){

'use strict';

app.on('start', function(){
     var view = new app.base.base_documents.views.DokumentenView();
     view.render();
});

})(window.app);

Is there a shorter and better way to write the follwing code

var view = new app.base.base_documents.views.CustomView();
view.render();

Maybe there is a way to call my view like this:

var view = CustomView();
view.render() 

My views.js

;(function (app) {

'use strict';

app.module('base.base_documents.views', function (module, App, Backbone, Marionette) {


    module.CustomView = Marionette.CompositeView.extend({
    ...
    });
}(app));

Solution

  • Depending on where your views are kept (namespace-wise), you could pass app.base.base_documents.views to the IIFE:

    ;(function(app, views){
    
      'use strict';
    
      app.on('start', function(){
           var view = new views.DokumentenView();
           view.render();
      });
    })(window.app, window.app.base.base_documents.views);
    

    But you may have views inside other nested namespaces, so for those you need to do the same - which in the end gives a similar result. You either step down through the namespaces when rendering the view, or you do it when passing the parameter.