How to detect invalid route and trigger function in Backbone.Controller

前端 未结 1 570
鱼传尺愫
鱼传尺愫 2020-12-19 05:23

Is there any method to detect invalid (or undefined) route and trigger 404 page in Backbone.Controller?

I\'ve defined routes in my Controller like this, but it didn\

相关标签:
1条回答
  • 2020-12-19 06:09

    The above works, but I'm not sure why you have to do what you do in the constructor. It may be slightly brittle, but we create a separate controller that we include in last. Its last so that the splat route is the last one to match:

    NotFound = Backbone.Controller.extend({
    
      routes: {
        "*path"  : "notFound"
      },
    
      notFound: function(path) {
        var msg = "Unable to find path: " + path;
        alert(msg);
      }
    
    });
    
    new NotFound();
    

    Using a more robust version of the above seems a cleaner approach to me.

    0 讨论(0)
提交回复
热议问题