Backbone.js路由失败

Backbone.js Routing failure

已更新

我需要 运行 index.html 的索引函数。我为 运行 处的函数做了 Router 想要做的所有事情 index.html 。这是我的 Router.js 。我想要 运行 index:function() 并在 index.html 上打印 "Hello World" 字符串。这样做的目的是将 router.js 连接到 index.html。如果我这样做,我会用新的 functions.I 来推进我的项目,希望我能告诉你。

感谢您的回答

    define([
    'jquery',
    'underscore',
    'backbone',
    'handlebars',
    'spin',
    'app/models/LoginModel',
    'app/collections/InformationCollection',
    'app/collections/TravelCollection',
    'app/collections/UserRegisterCollection',
    'app/collections/UserSearchCollection',
    'app/views/LoginView',
    'app/views/UserRegisterView'
], function ($,
             _,
             Backbone,
             Handlebars,
             Spinner,
             Login,
             Informations,
             Travels,
             UserRegister,
             UserSearch,
             LoginView,
             UserRegisterViews
) {


    var Router = Backbone.Router.extend({

        routes: {
            'search': 'search',
            'login': 'login',
            'travels': 'travels',
            'user': 'user',
            'menu': 'menu',
            '':'index'
        },

        index:function(){

            $(document.body).append("Hello World!..");
        },

这是我的 main.js

 require.config({

    baseUrl: "resources",
    waitSeconds: 100,
    paths: {
        'jquery': 'lib/jquery-1.9.1',
        'bootstrap': 'lib/bootstrap',
        'html5shiv': 'lib/html5shiv',
        'spin': 'lib/spin',
        'respond': 'lib/respond',
        'underscore': 'lib/underscore',
        'backbone': 'lib/backbone',
        'handlebars': 'lib/handlebars-v3.0.3',
        'template': 'app/templates'
    },
    shim: {
        html5shiv: {
            deps: ['jquery']
        },
        respond: {
            deps: ['jquery']
        },
        bootstrap: {
            deps: ['jquery']
        },
        jquery: {
            exports: '$'
        },
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        handlebars: {
            exports: "Handlebars"
        }
    },
    urlArgs: "bust=" + (new Date()).getTime()
});

require([
    'jquery',
    'bootstrap',
    'html5shiv',
    'respond',
    'util',
    'spin',
    'underscore',
    'backbone',
    'handlebars',
    'app/Router'
], function ($,
             $,
             $,
             $,
             util,
             Spinner,
             _,
             Backbone,
             Handlebars,
             Router) {
    var Router = new Router();
    Backbone.history.start();
});

我的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>


</body>
</html>

然后我 运行 完成了项目。index.html 只是清除没有 "Hello World" 的页面。

您需要实例化路由器,然后调用Backbone.history.start()

var router = new Router();
Backbone.history.start();

来自文档:

During page load, after your application has finished creating all of its routers, be sure to call Backbone.history.start() or Backbone.history.start({pushState: true}) to route the initial URL.