Marionette.js appRouter 未在应用启动时触发 [英] Marionette.js appRouter not firing on app start

查看:14
本文介绍了Marionette.js appRouter 未在应用启动时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将 Marionette 集成到现有的 Backbone 应用程序中.

I am currently integrating Marionette into an existing Backbone application.

我有一个现有的 Backbone 路由器,但我正在尝试实施 Marionette.AppRouter 来代替它.问题是,在新 Marionette 路由器应该选择的 url 上的硬刷新"上,它不会触发.如果我导航到另一个页面,然后返回到在硬刷新时未触发的 url,它会正确触发.我无法弄清楚为什么它在之后我导航到另一个页面并再次返回.这是我的代码示例:

I have an existing Backbone router in place, but am trying to implement a Marionette.AppRouter to take its place. The problem is that on a "Hard Refresh" on the url that the new Marionette router should pick up, it does not fire. If I navigate to another page, then go back to the url that did not fire on a hard refresh, it fires correctly. I cannot figure out why it works after I have navigated to another page and back again. Here is my code sample:

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 时,Marionette 路由器不会正常启动.

When I load the page: http://mysamplesite.com/#!/123/page directly, the Marionette router does not fire as it should.

但是,如果我加载页面:http://mysamplesite.com/#!/123 然后导航到 http://mysamplesite.com/#!/123/页面,Marionette 路由器正确触发.有什么想法吗?

However, if I load the page: http://mysamplesite.com/#!/123 and then navigate to http://mysamplesite.com/#!/123/page, the Marionette router fires correctly. Any ideas?

推荐答案

您是否在创建路由器实例后调用了Backbone.history.start()?如果没有,你需要这样做.在您的应用程序中调用它之前,路由器不会运行,并且在至少实例化一个路由之前您不能调用它.我通常这样做:

Did you call Backbone.history.start() after creating your router instance? If not, you need to do that. Routers won't run until this is called in your app, and you can't call this until at least one route has been instantiated. I usually do this:


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

这篇关于Marionette.js appRouter 未在应用启动时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆