简体   繁体   中英

Backbone router : url visited directly no route match

I am quite new to Backbone and I'm experiencing the following problem.

define([
'jquery',
'underscore',
'backbone',
'vm'
], function($, _, Backbone, Vm){
  'use strict';
  var AppRouter = Backbone.Router.extend({
    register: function(route, name, path) {

    this.route(route, name, function(){
      var args = arguments;
      require([path], function(module) {
        var options    = null,
            parameters = route.match(/[:\*]\w+/g);
        if (parameters) {
          options = {};
          _.each(parameters, function(name, idx) {
            options[name.substring(1)] = args[idx];
          });
        }
        var view = Vm.create(name, module, path);
        view.render();
      });
     });
    }
  });

  var initialize = function(){
     var appRouter = new AppRouter();

     appRouter.register('detail', 'detailView', 'views/detail/DetailView');

     appRouter.register('', 'HomeView', 'views/home/HomeView');

     Backbone.history.start({pushstate:true});
  };
  return {
    initialize: initialize
  };
});

Above there's my router, so the problem is that when I visit directly the url localhost/detail the route detail is not matched, instead the empty route is matched.

If I manually add a an # (localhost/#detail) to the url it works the correct route is matched. Could someone point me in the right direction, so that I will be able to load directly an url that is different from '/'

Actually the problem was just a typo

Backbone.history.start({pushstate:true});

changed to

Backbone.history.start({pushState:true});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM