2014-02-20 59 views
0

我的模塊代碼是。我該如何擺脫:TypeError:communityApp.activeTabLayout.pforum是undefined

spine.module( 'communityApp',{ startWithParent:假, 定義:功能(communityApp,APP,骨幹,木偶,$,_){ 的console.log( 「communityApp.js」);

控制器的
// Setup the router for this module 
    var Router = Marionette.AppRouter.extend({ 
     before: function() { 
      console.log("communityApp.js: router.before()") 
      App.startSubApp("communityApp", {}); 
     }, 
     appRoutes: { 
      "community": "showCommunityTab", 
      "community/pforum": "getPforum", 
      "community/questions":"getQuestions", 
      "community/events": "getEvents" 
     } 
    }); 

    // Startup router 
    App.addInitializer(function() { 
     console.log("communityApp.js: App.addInitializer()") 
     // contains active controller 
     communityApp.activeController = new communityApp.Controllers.tabController(); 
     // initializing route 
     communityApp.Router = new Router({ 
      controller: communityApp.activeController 
     }); 
    }); 

    // Let app know we started 
    communityApp.addInitializer(function() { 
     console.log("communityApp.js: DemoApp.addInitializer()"); 
     App.vent.trigger("app:started", "communityApp"); 
    }); 

    // This will run when a sub app (module) starts 
    communityApp.on("start", function() { 
     console.log("communityApp.js: on 'Start'()"); 
    }); 

代碼是

spine.module( 「communityApp」,功能(communityApp,APP,骨幹,木偶,$,_){ 「使用嚴格」。

// initializing controllers and collections for message tabs 
communityApp.Controllers = {}; 
communityApp.Collections = {}; 

communityApp.Controllers.tabController = Marionette.Controller.extend({ 
    showCommunityTab: function() { 
     this.getCommunityTab(); 
    }, 
    getCommunityTab: function (data) { 
     //var tabLayout = new communityApp.Views.tabLayout(); 
     //tabLayout.render(); 
     // creating active layout 
     communityApp.activeTabLayout = new communityApp.Views.tabLayout(); 
     communityApp.activeTabLayout.render(); 
     // loading community module view 
     App.regionMain.show(communityApp.activeTabLayout); 
     // load pforum on community module load 
     this.getPforum(); 
    }, 
    getPforum : function(){ 
     console.log('Public Forum Tab'); 
     var pforum = new communityApp.Controllers.pforumController(); 
     pforum.init(); 

    }, 
    getQuestions : function(){ 
     console.log('Question tab'); 
     var questions = new communityApp.Controllers.questionsController(); 
     questions.init(); 

    }, 
    getEvents : function(){ 
     console.log('Events tab'); 
     var events = new communityApp.Controllers.eventController(); 
     events.init(); 

    } 
}); 

錯誤代碼,它是一個標籤頁。

spine.module( 「communityApp」 功能(communityApp,應用程序,骨幹,木偶,$ _){ 「使用嚴格的」;

communityApp.Controllers.pforumController = Marionette.Controller.extend({ 
    init: function(){ 
     var func = _.bind(this._getPforum, this); 
     $.when(App.request('alerts1:entities' , {origin:'pforum'})) 
      .then(func) 
    }, 
    _getPforum:function(data){ 

     // populating the data 
     communityApp.activeTabLayout.pforum.show(new communityApp.CollectionViews.pforumCollectionViews({ 
      collection: data 
     })); 
    } 
}); 

回答

0

那是因爲你正在創建的activeTabLayoutgetCommunityTab,並且你想在getPforum創建控制器後才能訪問它。這些都連接到不同的路線。所以你沒有保證的廣告是communityApp.activeTabLayout存在於您的init方法裏面。

community/pforum - >創建你的控制器,但不是你的標籤佈局。

community - >創建您的標籤佈局,但不是您的控制器。

您需要確保communityApp.activeTabLayout = new communityApp.Views.tabLayout();之前運行pforum.init();