Backbone router 未生效可能情况

define(['backbone'],function(Backbone){
 	var navController = Backbone.Router.extend({
 		routes : {
 			'test':'test',
 			'nav/:path': 'gotoNav'
		},
		initialize:function(){//constructor
			console.log(123);
		},
		test:function(){alert(1)},
		gotoNav:function(path){
			console.log(path);
		}
 	});
 	return navController;
})

今天写一个backbone router, 结果发现routes没有生效, 由于长时间没用了  

开始时候发现

情况1:

Backbone.history.start() 忘记调用了

情况2:

手贱,心血来潮把初始化方法写成 constructor 结果不生效,修改 initialize 后正常工作

constructor / initialize new Router([options]) 
When creating a new router, you may pass its routes hash directly as an option, if you choose. All options will also be passed to your initialize function, if defined.

我XX 我还以为这2个方法是同样效果的呢, 现在看来  initialize 应该是backbone constructor 的回调函数(猜测啊) 闲情可以看backbone源码

记录一下吧,省的以后又忘记了




你可能感兴趣的:(backbone,router)