恩伯:如何处理意外路线?

cyc*_*arc 2 ember.js

如果路由存在,路由器会很好地处理URL并导致调用适当的路由.但是如何处理路径不存在的情况?

app.js

App = Ember.Application.create();

App.Router.map(function () {
  this.resource('about', { path: "/about" });
  this.resource('admin', { path: "/admin" });
});
Run Code Online (Sandbox Code Playgroud)

的index.html

<script type="text/x-handlebars" id="about">
      <h1>ABOUT</h1>
</script>

<script type="text/x-handlebars" id="admin">
      <h1>ADMIN</h1>
</script>
Run Code Online (Sandbox Code Playgroud)

使用案例:

当用户输入URL index.html#/ xxxx时,我想转换到一种错误页面,指示所请求的"页面"不会退出.

到目前为止,我没有找到解决方案......

mav*_*ein 8

您可以像这样添加一个捕获所有Route:

this.route('missing', { path: "/*path" });
Run Code Online (Sandbox Code Playgroud)

然后在此路线内重定向.我的实现如下所示:

App.MissingRoute = Ember.Route.extend({
    redirect : function(){
        App.LOG.warn("No Route for given URL found. Will transition to Index Route instead.");
        this.transitionTo("index");
    }
});
Run Code Online (Sandbox Code Playgroud)