我正在关注使用模块组织骨干网教程,除了我必须进行一些调整以适应自文章编写以来对依赖项的更改之外,我无法让我的 .on() 事件在何时触发路由匹配。
如果您查看索引路由器,您会看到一个警报和一个 console.log()。页面加载时也不会触发。也没有js错误。
任何帮助将不胜感激。
路由器.js
define([
'jquery',
'underscore',
'backbone',
'views/index',
'views/ideas'
], function($, _, Backbone, IndexView, IdeasView) {
var AppRouter = Backbone.Router.extend({
'': 'index',
'/ideas': 'showIdeas',
'*actions': 'defaultAction'
});
var initialize = function() {
console.log('this works so i know initialize() is being called');
var app_router = new AppRouter;
// not firing
app_router.on('route:index', function() {
alert('hi');
console.log('hi');
// var index_view = new IndexView();
// index_view.render();
});
// not firing
app_router.on('route:showIdeas', function() {
console.log('showIdeas');
var ideas_view = new IdeasView();
});
//not firing
app_router.on('route:defaultAction', function(actions) {
console.log('No route:', actions);
});
if (!Backbone.history.started ) {
Backbone.history.start();
console.log( "Route is " + Backbone.history.fragment );
}
};
return {
initialize: initialize
};
});