[英]backbone.js router not responding to url fragments
I have a simple router defined and instantiated. 我有一个定义和实例化的简单路由器。
Problem: When I go to the url http://localhost/backbone1/#photos/5
, I expect to see an output from console.log()
in the javascript console, but nothing shows up. 问题:当我进入url http://localhost/backbone1/#photos/5
,我希望在javascript控制台中看到console.log()
的输出,但是什么都没有显示。 Did i miss out on something? 我错过了什么吗?
JS Code 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();
You are missing a initialize method 您缺少初始化方法
var initialize = function () {
var myGalleryRouter = new GalleryRouter();
Backbone.history.start();
};
return {
initialize: initialize
};
Router starts listening to url hash changes after you call Backbone.history.start()
. 在调用Backbone.history.start()
之后,Router开始侦听url哈希更改。 Usually, you'd want to load essential data and initialize global views first, then start router. 通常,您需要先加载基本数据并初始化全局视图,然后再启动路由器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.