2014-09-22 61 views
0

我有一個控制器,我開始我的木偶應用程序並定義一個主要區域。在我的主區域內,有一個mainLayout進一步包含兩個子區域,我正在渲染這兩個子區域,但onShow/onRender函數不會被調用。 :Marionette Layout的onShow功能沒有被調用

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 
    } 

} 

回答

2

你從來沒有真正渲染你使用EventView。該方法昂秀將被調用,如果你真的在你的應用程序做這個渲染使用EventView:

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

this.mainRegion.show(view); 

如果您希望您使用EventView包含另一種觀點認爲,那麼你應該使它的佈局。你可以將你的佈局和你的eventView結合在一起。如果你不想讓你的事件視圖呈現任何東西,那麼只需使它成爲一個控制器,並將onShow函數放在佈局上。