路线上不会触发木偶事件

时间:2014-09-22 13:05:20

标签: marionette

为什么在访问网址时未触发木偶事件?

当我访问URL时,它会调用函数API.goHome()

但它没有!我不明白为什么?

这是我的代码:

App.js

var PatientPortal = new Marionette.Application();

PatientPortal.addRegions({
    'headerRegion': '#header',
    'bodyRegion': '#body',
    'footerRegion': '#footer'
});

PatientPortal.on("before:start", function () {

    console.log("Started");

});

PatientPortal.navigate = function (route, options) {
    options || (options = {});
    Backbone.history.navigate(route, options);
};

PatientPortal.getCurrentRoute = function () {
    return Backbone.history.fragment
};

PatientPortal.on("start", function(){

    if (Backbone.history) {
        Backbone.history.start();
    }

    if(PatientPortal.getCurrentRoute() == ""){
        PatientPortal.navigate('home');
    }


});

PatientPortal.start();

和路由器代码:

PatientPortal.module("Portal", function (Portal, PatientPortal, Backbone, Marionette, $, _) {
    Portal.Router = Backbone.Marionette.AppRouter.extend({
        controller: "API",
        appRoutes: {
            "": "goHome",
            "home": "goHome"
        }
    });

    var API = {
        goHome: function () {
            console.log("go home");
        }
    };

    PatientPortal.on("home:route", function () {
        console.log("OKOKOK")
        API.goHome();
    });

    PatientPortal.addInitializer(function () {
        new Portal.Router({
            controller: API
        });
    });
});

这里有一个主页代码:

<!DOCTYPE html>
<html>
<head>
    <title>INSAH - Login Page</title>
</head>
<body>

<div id="header"></div>

    <script src="./js/assets/provider/jquery/jquery-2.1.1.min.js"></script>
    <script src="./js/assets/provider/underscore/underscore-min.js"></script>
    <script src="./js/assets/provider/backbone/backbone-min.js"></script>
    <script src="./js/assets/provider/backbone/backbone.babysitter.min.js"></script>
    <script src="./js/assets/provider/backbone/backbone.wreqr.min.js"></script>
    <script src="./js/assets/provider/marionette/backbone.marionette.min.js"></script>


    <script src="./js/app.js"></script>
    <script src="./js/routes/route.js"></script>

</body>
</html>

感谢。

1 个答案:

答案 0 :(得分:0)

我想出了问题,这是因为启动了主干历史记录,AddInitializer功能应该如下:

PatientPortal.addInitializer(function () {
    new Portal.Router({
        controller: API
    });
    Backbone.history.start();
});

我们应该从app.js中删除这一行:

    Backbone.history.start();