CRE*_*REO 2 backbone.js marionette
我目前正在将Marionette集成到现有的Backbone应用程序中.
我有一个现有的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();
Run Code Online (Sandbox Code Playgroud)
当我加载页面时:http://mysamplesite.com/#!/123/page直接,Marionette路由器不会按原样加载.
但是,如果我加载页面:http://mysamplesite.com/#!/123然后导航到http://mysamplesite.com/#!/123/page,Marionette路由器将正常启动.有任何想法吗?
你Backbone.history.start()在创建路由器实例后打电话了吗?如果没有,你需要这样做.在您的应用程序中调用此路由器之前,路由器不会运行,并且在实例化至少一个路由之前无法调用此路由器.我经常这样做:
TestApp.on("initialize:after", function(){
if (Backbone.history){ Backbone.history.start(); }
});
Run Code Online (Sandbox Code Playgroud)
心连心