2

我一直在尝试在backbone.js上练习,我在网上找到了一个我试图遵循的教程(http://backbonetutorials.com/what-is-a-router/)但不幸的是我无法达到我的观点通过路由器。

下面是我的代码

main.js

requirejs.config({
    // create local alias for package
    paths: {
        l           :   'my/vehicle',
        underscore  :   'vendors/underscore',
        jqueryui    :   'vendors/jquery-ui/js/jquery-ui-1.9.0.custom.min',
        backbone    :   'vendors/backbone',
        bootstrap   :   'vendors/bootstrap'

    },
    shim: {
        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        },

        underscore: {
            exports: "_"
        }
    }

})


require(['../../config'], function(core){

    require(["l/app"], function(App) {

        'use strict';
        App.initialize();

    });

});

应用程序.js

define(["jquery", "backbone", "l/router"], function($, Backbone, Router) {

    var initialize = function(){
        // Pass in our Router module and call it's initialize function

        Router.initialize();

    }

    return {
        initialize: initialize
    };
});

路由器.js

define([
    'jquery',
    'underscore',
    'backbone',
    'l/views/BrowseVehicle'
], function($, _, Backbone, BrowseVehicleView){

    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
            '/browse' : 'showVehicleBrowse',

            // Default
            '*actions'  : 'defaultAction'
        }
    });

    var initialize = function(){
        var app_router = new AppRouter;
        app_router.on('showVehicleBrowse', function(){
            // Call render on the module we loaded in via the dependency array
            console.log('am here');
            var BrowseVehicleView = new BrowseVehicleView();
            BrowseVehicleView.render();
        });

        app_router.on('defaultAction', function(actions){
            // We have no matching route, lets just log what the URL was

            console.log('No route:', actions);
        });

        Backbone.history.start();
    };
    return {
        initialize: initialize
    };
});

意见/BrowseVehicle.js

define([
    'jquery',
    'underscore',
    'backbone'
], function($, _, Backbone){
    var BrowseVehicleView = Backbone.View.extend({
        el: $('#vehicle-browse-form'),
        render: function(){
            // Using Underscore we can compile our template with data
            console.log('I reached vehicle browse form');
        }
    });
    // Our module now returns our view
    return BrowseVehicleView;
});

加载代码没有错误,console.log 没有在视图中打印任何内容,也没有在路由函数内的路由器中打印任何内容。我尝试使用 URL/#/browse 访问我的 url,但没有得到 console.log 语句。

有人可以请教吗?

4

1 回答 1

1

在 routes {} 定义中删除浏览前面的正斜杠。

于 2012-10-20T13:20:38.087 回答