Marionette Layout的onShow功能没有被调用

时间:2014-09-22 05:28:04

标签: backbone.js marionette backbone-views

我有一个控制器,我正在启动我的牵线木偶应用程序并定义一个主要区域。在我的主要区域内,有一个mainLayout,它进一步包含两个子区域,我正在渲染这两个子区域,但我的主项目视图的onShow / onRender函数不会被调用。 :

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();
    }
}

现在我对这个应用程序的相应项目视图如下:

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
    }

}

1 个答案:

答案 0 :(得分:2)

您永远不会实际渲染您的eventView。如果您在应用程序中实际渲染eventView,则会调用onShow方法:

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

this.mainRegion.show(view);

如果您希望eventView包含另一个视图,那么您应该将其设置为布局。您可以将布局和eventView组合在一起。如果您不希望eventView呈现任何内容,那么只需将其设为控制器并将onShow函数放在布局上即可。