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

时间:2013-08-06 15:13:24

标签: ember.js

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

app.js

App = Ember.Application.create();

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

的index.html

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

<script type="text/x-handlebars" id="admin">
      <h1>ADMIN</h1>
</script>

使用案例

当用户输入URL index.html#/ xxxx时,我想转换到某种错误页面,表明所请求的“页面”不会退出。

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

2 个答案:

答案 0 :(得分:8)

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

this.route('missing', { path: "/*path" });

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

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");
    }
});

答案 1 :(得分:3)

如果您正在寻找其他方法,

Ember.JS: How to handle invalid URLs就是一个很好的例子。