我使用下面的路由器在我的應用程序:爲什麼我的骨幹路由器重定向到應用根目錄?
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});
它成功地將應用程序服務器/#文件夾,但是當我通過
app.router.navigate("folders/" + id, {trigger: true, replace: true});
它觸發應用程序的根目錄並加載我的登錄頁面。在重定向發生之前,我可以看到正確的URL(例如server/folders/folder1)暫時閃爍。任何關於這裏發生的事情的想法?
我不知道是什麼問題,但我認爲[jQuery的移動路由器(https://github.com/azicchetti/jquerymobile-router)插件最初創建,因爲JQM沒發揮好與backbone.js。 – Jack