Javascript 无法使Backbone.js路由处理程序在匹配的路由上启动

Javascript 无法使Backbone.js路由处理程序在匹配的路由上启动,javascript,backbone.js,Javascript,Backbone.js,我一直在遵循这一原则,除了自撰写本文以来为适应对依赖项的更改而进行的一些调整外,我无法在匹配路由时触发.on()事件 如果您查看索引路由器,您将看到一个警报和一个console.log()。页面加载时也不会触发。也没有js错误 任何帮助都将不胜感激 router.js define([ 'jquery', 'underscore', 'backbone', 'views/index', 'views/ideas' ], function($, _, Backbone, I

我一直在遵循这一原则,除了自撰写本文以来为适应对依赖项的更改而进行的一些调整外,我无法在匹配路由时触发.on()事件

如果您查看索引路由器,您将看到一个警报和一个console.log()。页面加载时也不会触发。也没有js错误

任何帮助都将不胜感激

router.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
  };
});

确保将实际路由放入路由器定义上的路由哈希中:

var AppRouter = Backbone.Router.extend({
    routes: {
        '': 'index',
        '/ideas': 'showIdeas',
        '*actions': 'defaultAction'
    }
});
我还想补充一点,我更喜欢将路由的回调放在路由器定义中(这只是一个首选项):


这不是必需的,但对我来说,它更容易阅读和了解发生了什么。

一个url不触发您的某个路由的示例是什么?或者更具体地说,完全忽略了这一点。接得好。谢谢
var AppRouter = Backbone.Router.extend({
    routes: {
        '': 'index',
        '/ideas': 'showIdeas',
        '*actions': 'defaultAction'
    },

    index: function () {
        // function body here
    },

    showIdeas: function () {
        // function body here
    },

    defaultAction: function () {
        // function body here
    }
});