0

我在我的应用程序中使用以下路由器:

app.Router = Backbone.Router.extend({
    // define the route and function maps for this router
    routes : {
        "" : "showLogin",
        // Sample usage: http://ServerManager/
        "login" : "login",
        // Sample usage: http://ServerManager/#login
        "logout" : "logout",
        // Sample usage: http://ServerManager/#logout
        "folders" : "listFolders",
        // Sample usage: http://ServerManager/#folders
        "folders/:name" : "showFolder",
        // Sample usage: http://ServerManager/#folders/System
        "*other" : "defaultRoute"
        // Sample usage: http://ServerManager/#badHash
    },
    showLogin : function() {
        console.log("Home page loaded. Redirecting to #login page.");
        this.navigate("login", {trigger: true, replace: true});
    },
    login : function() {
        //console.log("login route fired");
        // delete sessionStorage items and load a new loginview
        this.changePage(new app.LoginView()); 
    },
    logout : function() {
        //console.log("logout route fired");
        // Clear the token and other session items from sessionStorage
        sessionStorage.clear();
        this.navigate("login", {trigger: true, replace: true});
    },
    listFolders : function() {
        //console.log("listFolders route fired.")
        if (app.folderView === undefined) {
            console.log("creating new app.folderView");
            app.folderView = new app.FolderView();
        }
        else {
            console.log("using existing app.folderView");
        }
        this.changePage(app.folderView);
    },
    showFolder : function(name) {
        console.log( app.router.routes[Backbone.history.fragment] );
        //console.log("showFolder route fired.")
        this.changePage(new app.ServiceView({folder : name}));
        console.log( app.router.routes[Backbone.history.fragment] );
        //app.router.navigate("folder/" + name, {trigger: false});
    },
    defaultRoute : function() {
        alert("Error. Page doesn\'t exist.");
    },
    changePage : function(page) {
        $(page.el).attr("data-role", "page");
        page.render();
        $("body").append($(page.el));
        var transition = $.mobile.defaultPageTransition;
        $.mobile.changePage($(page.el), {changeHash : true, transition : transition});
    }
});

如果我使用

app.router.navigate("folders", {trigger: true, replace: true});

它成功地将应用程序发送到服务器/#folders,但是当我通过

app.router.navigate("folders/" + id, {trigger: true, replace: true});

它触发应用程序的根目录并加载我的登录页面。在重定向发生之前,我可以看到正确的 URL(例如 server/folders/folder1)暂时闪烁。关于这里发生了什么的任何想法?

4

1 回答 1

1

骨干路由器最后期望更通用的路由。

设置folders/:id路线后跟folders路线。

更新

其实仔细想想,也未必是这样的。你可以发布控制台输出吗?它调用了正确的函数吗?

也许$.mobile ... changeHash: true是矛盾的。您可能应该只让其中一个框架为您更改 url 哈希。

于 2012-09-07T20:03:34.030 回答