Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marionette - Relationships between Application and Module

We are currently building a Marionette based application. Basically, we have a Marionette Application that has multiple regions defined on it. Each region will act as a container for different Modules to display their views. I want each Module to have full control of what is being displayed in it's container, but I want the Application to allocate these regions. For simplicity, let's say that each module just has a simple ItemView.

I'm considering 2 approaches to populating those regions with the module views.

The first approach says that when each module is initialized, it will create its view and it will call the application to display its view in the specified region, for example:

var app = new Marionette.Application();
app.addRegions({
    regionA: "#regionA",
    regionB: "#regionB"
});

app.module("moduleA", function(moduleA, app, ...){
    moduleA.on("start", function(){
        var viewA = new MyViewA();
        app.regionA.show(viewA);
    }
});

app.module("moduleB", function(moduleB, app, ...){
    moduleB.on("start", function(){
        var viewB = new MyViewB();
        app.regionB.show(viewB);
    }
});

The second approach says that each module should expose some function that returns its view. The Application will call that function when ready and it will stick the view in the designated region.

I'm not sure which approach is better and would be happy to hear opinions.

like image 588
elanh Avatar asked Feb 20 '13 16:02

elanh


1 Answers

I would definitely go with the second approach, after having gone with the first approach in the past I am now hitting the limitations of this approach and moving to the second approach. I wrote a blog post about it here

like image 112
martin308 Avatar answered Nov 05 '22 21:11

martin308