EmberJS路由模型收到404时 [英] EmberJS route for model when receives a 404

查看:77
本文介绍了EmberJS路由模型收到404时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下路线:

  App.IndexCrmPersonEditRoute = Ember.Route.extend({
model:function(params){
var person = this.store.find(person,params .id);
return person;
},
});

其中收到以下路由器路径

  ... 
this.route('edit',{path:':id'}); //编辑人
...

这对现有人来说很好。当对象不存在时,API返回一个包含以下内容的404:

  {person:null} 

一个错误加载路由时出错:未定义发生在JS中。 / p>

我想知道如果返回404 + person = null是正确的方法,如果可以显示消息并将用户重定向到之前的URL(甚至



任何想法?

解决方案

嗯,感谢上面的两个答案,我发现路由器的错误动作的解决方案,我在下面描述。



http://emberjs.com/guides / routing / load-and-error-substates /#toc_code-error-code-substates-with-dynamic-segments



文档不是很好但是这是我实现的:



我写了一个通用的Route类,用error操作进行扩展:

  App.NotFoundObjectRoute = Ember.Route.extend({
actions:{
error:function(reason,transition){
if (reason.status == 404)
return true
else
this.transitionTo('r500');
},
},
});

我预计每个路由都会发现未找到beheaviour,我写和错误路由,如下(注意自动替换编辑一词,如EmberJS的标准:

  App.IndexCrmPersonEditRoute = App。 NotFoundObjectRoute.extend({
// ...
});

App.IndexCrmPersonErrorRoute = Ember.Route.extend({
//被称为错误, 404(未找到)和500(服务器错误)
});

也写一个错误视图,如下所示:

  App.IndexCrmBusinessErrorView = Em.View.extend({
templateName: r404,
});

我使用标准的r404模板,但是您可以看到它可能是每个不同对象类型的不同的。



r500路由在服务器错误(404除外)的情况下起作用。



我希望对他人有帮助。


(This same text is open in EmberJS Discuss area too)

I have the following Route:

App.IndexCrmPersonEditRoute = Ember.Route.extend({
    model: function(params) {
        var person = this.store.find("person", params.id);
        return person;
    },
});

which receives the following Router path

...
this.route('edit', { path: ':id'}); // edit person
...

That works nicely for existing people. When the object doesn't exist, the API returns a 404 with the following content:

{"person": null}

an error "Error while loading route: undefined " happens in JS.

I wish to know if returning 404 + person = null is the correct approach and if it is possible to show a message and redirect the user back to the previous URL (or even not move them if the response is 404).

Any idea?

解决方案

Well, thanks to the 2 answers above, I found out the solution with the Router's "error" action, which I describe below.

http://emberjs.com/guides/routing/loading-and-error-substates/#toc_code-error-code-substates-with-dynamic-segments

The documentation is not very good but here is what I made to implement:

I wrote a generic Route class to be extended with the "error" action:

App.NotFoundObjectRoute = Ember.Route.extend({
    actions: {
        error: function(reason, transition) {
            if (reason.status == 404)
                return true
            else
                this.transitionTo('r500');
        },
    },
});

Each route I expect to have the "Not found" beheaviour, I wrote and Error route, as below (attention to the word "Error" that automatically replaces "Edit", as EmberJS' standard:

App.IndexCrmPersonEditRoute = App.NotFoundObjectRoute.extend({
  // ...
});

App.IndexCrmPersonErrorRoute = Ember.Route.extend({
  // Called for errors like 404 (not found) and 500 (server error)
});

That turned to write also an error view, as below:

App.IndexCrmBusinessErrorView = Em.View.extend({
    templateName: "r404",
});

I used the standard "r404" template for that, but as you can see it could be a different one for each different object type.

The "r500" route acts in case of server error (other than 404).

I hope that can be helpful for others.

这篇关于EmberJS路由模型收到404时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆