1

我定义并实例化了一个简单的路由器。

问题:当我转到 urlhttp://localhost/backbone1/#photos/5时,我希望在 javascript 控制台中看到输出console.log(),但没有显示任何内容。我错过了什么吗?

JS代码

var GalleryRouter = Backbone.Router.extend({

    routes: {
        "about": "showAbout",
        "photos/:id": "getPhoto",
        "search/:query": "searchPhotos",
        "search/:query/p:page": "searchPhotos",
        "photos/:id/download/*imagePath": "downloadPhoto",
        "*other": "defaultRoute"
    },

    showAbout: function() {

    },

    getPhoto: function(id) {
        console.log('You are trying to reach photo ' + id);
    },

    searchPhotos: function(query, page) {
        console.log('Page number: ' + page + ' of the results for ' + query);
    },

    downloadPhoto: function(id, imagePath) {

    },

    defaultRoute: function(other) {
        console.log("Invalid. You attempted to reach: " + other);
    }



});

var myGalleryRouter = new GalleryRouter();
4

2 回答 2

1

您缺少初始化方法

    var initialize = function () {
            var myGalleryRouter = new GalleryRouter();
            Backbone.history.start();
        };
return {
        initialize: initialize
    };
于 2012-06-12T16:57:29.260 回答
0

调用后,路由器开始监听 url 哈希变化Backbone.history.start()。通常,您需要先加载基本数据并初始化全局视图,然后再启动路由器。

于 2012-06-12T19:40:29.103 回答