[英]backbone.js router not responding to url fragments
我有一个定义和实例化的简单路由器。
问题:当我进入url http://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();
您缺少初始化方法
var initialize = function () {
var myGalleryRouter = new GalleryRouter();
Backbone.history.start();
};
return {
initialize: initialize
};
在调用Backbone.history.start()
之后,Router开始侦听url哈希更改。 通常,您需要先加载基本数据并初始化全局视图,然后再启动路由器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.