2013-03-06 31 views
2

我目前正在將Marionette集成到現有的Backbone應用程序中。Marionette.js appRouter未在應用程序啓動時觸發

我有一個現有的Backbone路由器,但我試圖實現一個Marionette.AppRouter取代它的位置。問題是,在新的Marionette路由器應該拾取的URL上進行「硬刷新」時,它不會觸發。如果我導航到另一個頁面,然後返回到硬刷新時未觸發的URL,它會正確觸發。我不知道爲什麼它可以工作我已經導航到另一個頁面,然後再回來。這裏是我的代碼示例:

TestApp = new Backbone.Marionette.Application(); 
    var testAppController = { 
     testLoadPage: function(){ 
      console.log('testLoadPage Fired'); //<---- DOES NOT FIRE ON HARD REFRESH 
     } 
    }; 
    TestAppRouter = Backbone.Marionette.AppRouter.extend({   
     appRoutes: { 
      "!/:var/page": "testLoadPage", 
      "!/:var/page/*path": "testLoadPage" 
     }, 
     controller: testAppController, 
     route: function(route, name, callback) { 
      return Backbone.Router.prototype.route.call(this, route, name, function() { 
       if (!callback) callback = this[name]; 
       this.preRoute(); 
       this.trigger.apply(this, ['beforeroute:' + name].concat(_.toArray(arguments))); 
       callback.apply(this, arguments); 
      }); 
     }, 
     preRoute: function() { 
      app.showLoader(name, arguments); 
     } 
    }); 
    TestApp.addRegions({ 
     contentContainer: '#container' 
    }); 
    TestApp.addInitializer(function(options){ 
     new TestAppRouter(); 
    }); 
    TestApp.start(); 

當我加載頁面:http://mysamplesite.com/#!/123/page直接,木偶路由器不火,因爲它應該。

但是,如果我加載頁面:http://mysamplesite.com/#!/123,然後導航到http://mysamplesite.com/#!/123/page,則Marionette路由器會正確觸發。有任何想法嗎?

回答

7

在創建路由器實例後,您是否撥打Backbone.history.start()?如果沒有,你需要這樣做。在你的應用中調用路由器之前,路由器不會運行,並且至少有一條路由被實例化之前,你不能調用它。我經常這樣做:


TestApp.on("initialize:after", function(){ 
    if (Backbone.history){ Backbone.history.start(); } 
}); 

心連心

+0

是的,我有'Backbone.history.start'調用。我試着在示例代碼塊的開始處調用它,在示例代碼塊之後,我剛剛在示例代碼塊的末尾嘗試了您的方法,但無濟於事。 'console.log('testLoadPage');'仍然不會觸發。感謝您的回覆,但問題仍然存在。 – CREO 2013-03-07 17:37:13

+0

我也注意到您的回覆中的修復不會觸發。我添加的代碼:'TestApp.on( 「初始化:後」,函數(){ \t \t \t如果(Backbone.history){ \t \t \t \t的console.log( 'startingHistoryAgain') \t \t \t \t骨幹.history.start(); \t \t \t}其他( \t \t \t \t的console.log( '未再次啓動') \t \t \t) \t \t});'既不是,也不是被解僱。 @Derick Bailey – CREO 2013-03-07 18:21:11

+3

請確保在調用TestApp.start();' – 2013-03-08 03:32:54

相關問題