我目前正在使用骨干样板并在我的MAMP服务器localhost上运行它。我似乎无法让路由器工作
require([
// Global
"app",
// Libs
"jquery",
"backbone"
],
function(app, $, Backbone) {
// Defining the application router, you can attach sub routers here.
var Router = Backbone.Router.extend({
routes: {
"photo" : "getPhoto",
"*other" : "index"
},
index: function()
{
console.info('Index Function Working');
},
getPhoto: function()
{
console.log("You are trying to reach photo ");
}
});
// Treat the jQuery ready function as the entry point to the application.
// Inside this function, kick-off all initialization, everything up to this
// point should be definitions.
$(function() {
// Define your master router on the application namespace and trigger all
// navigation from this instance.
app.router = new Router();
console.info('Here');
// Trigger the initial route and enable HTML5 History API support
Backbone.history.start({ pushState: true });
});
// All navigation that is relative should be passed through the navigate
// method, to be processed by the router. If the link has a `data-bypass`
// attribute, bypass the delegation completely.
$(document).on("click", "a:not([data-bypass])", function(evt) {
// Get the anchor href and protocol
var href = $(this).attr("href");
var protocol = this.protocol + "//";
// Ensure the protocol is not part of URL, meaning it's relative.
if (href && href.slice(0, protocol.length) !== protocol &&
href.indexOf("javascript:") !== 0) {
// Stop the default event to ensure the link will not cause a page
// refresh.
evt.preventDefault();
// `Backbone.history.navigate` is sufficient for all Routers and will
// trigger the correct events. The Router's internal `navigate` method
// calls this anyways.
Backbone.history.navigate(href, true);
}
});
});
它只是不断触及索引功能。
我把它放到我的网址中 “http://localhost:8888/OutfitApp/#photo *”和“http://localhost:8888/OutfitApp/photo”
两者都不起作用。
我删除了
"*other" : "index"
没有任何东西出现在我的控制台中。
我很困惑我错过了什么?
答案 0 :(得分:3)
来自docs:
如果您的申请未通过您的根网址提供 域名,请务必告诉历史根目录的真实位置 选项: Backbone.history.start({pushState:true,root:“/ public / search /”})
这可能是问题吗?
在您的情况下,请尝试:
Backbone.history.start({pushState: true, root: "/OutfitApp/"})