Kavish Dwivedi
Kavish Dwivedi

Reputation: 121

Marionette Layout's onShow function not getting called

I have a controller where I am starting my marionette app and defining a main region. Inside my main region, there is a mainLayout which further contains two sub-regions which i am rendering these two subregions but the onShow/onRender functions for my main itemview don't get called. :

var eventController = my.class{
    create: function(){
        //This is the model
        var mainModel = new model();

        this.createEventApp = new Backbone.Marionette.Application();
        this.createEventApp.addRegions({
            //This is the main container 
            mainRegion: ".event-container"
        });

        this.createEventApp.addInitializer(function(options){
            var self=this;
            //This is the main Item View for the app
            new EventView({
                model: mainModel,
                region: self.mainRegion
            });
        });
        this.createEventApp.start();
    }
}

Now my corresponding Item View to this application is like :

var eventView = Backbone.Marionette.ItemView.extend{
    //Template for the item view
    template: eventViewTemplate;

    initialize: function(options){
         _.bindAll(this);
        this.model = options.model;
        this.region = options.region;

        //Creating a main layout inside mainRegion of the app
        var myLayout = Backbone.Marionette.Layout.extend({
            template: EventViewTemplate,
            regions: {
                //Region 1
                //Region 2

            }
        });

        this.mainLayout = new myLayout();
        this.region.show(this.mainLayout);
        this.region1view = new region1({
            model: this.model
        });
        //same for region 2

        //showing both the regions in the layout
        this.mainLayout.region1.show(this.region1view);
        this.mainLayout.region2.show(this.region2view);
    },

    onShow: function(){
         //I want to do something here but this function doesn't event gets called
    }

}

Upvotes: 0

Views: 1655

Answers (1)

Ryan Silva
Ryan Silva

Reputation: 965

You are never actually rendering your eventView. The onShow method will get called if you actually render the eventView by doing this in your application:

var view = new EventView({
    model: mainModel
});

this.mainRegion.show(view);

If you want your eventView to contain another view then you should make it the layout. You can probably combine your layout and your eventView together. If you don't want your eventView to render anything then just make it a controller and put the onShow function on the layout.

Upvotes: 2

Related Questions