Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding layouts in Marionette for Backbone.js

I think I may have a fundamental misunderstanding of how Marionette.Layout is intended to be used.

I'm trying to something like this:

enter image description here

The layout includes two Marinotette.ItemViews: The "Explode" ItemView and the "PopStar" ItemView. This layout is designed to always contain these views, so I tried to do this:

var TheLayout = Backbone.Marionette.Layout.extend({
    template: '#the=layout-template',
    regions: {
        explode: '#explode-region',
        popstar: '#popstar-region'
    }
    initialize:function(options){
        _.bindAll(this);

        var explodeView = new ExplodeView();
        this.explode.show(explodeView);        // <-- This throws and exception because the regions are not available yet

    }
})

But it looks like the regions are not available until after the layout is rendered. I tried calling this.render() before adding the views in, but this didn't work. I'm pretty sure the fundamental problem here is that I'm applying the layout in the wrong circumstance.

What should I be doing in this circumstance? When is the correct time to use Marionette.Layout?

Thanks!

like image 716
Chris Dutrow Avatar asked Aug 15 '12 17:08

Chris Dutrow


1 Answers

Show the region views in the layout's onRender method. Code:

var TheLayout = Backbone.Marionette.Layout.extend({
    template: '#the=layout-template',
    regions: {
        explode: '#explode-region',
        popstar: '#popstar-region'
    }
    onRender: function() {
        var explodeView = new ExplodeView();
        this.explode.show(explodeView);
    }
})

Note that in this case the _.bindAll(this) is not needed.

like image 100
Tony Abou-Assaleh Avatar answered Nov 15 '22 19:11

Tony Abou-Assaleh