如何处理路由中的垃圾数据404? [英] How to handle 404 of Ember Data in route?

查看:71
本文介绍了如何处理路由中的垃圾数据404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的路线上,我有一种方法,尝试从服务器请求一个模型列表。

In my route I have a method that tries to request a list of models from the server

 model: ->
    App.MyModel.find
      projectId: (@modelFor "project").id



现在显然有时候可能会返回404。

Now obviously sometimes this might return a 404.

在这种情况下,Ember只是停止做任何事情。

At the moment when this happens, Ember just stops doing anything. No view is rendered, no controller is setup.

那么如何正确处理404(即显示错误视图)?

推荐答案

坏消息:现在,当在find()上获取404时,ember-data不会做任何事情。在所有该模型永远处于'加载'状态。

Bad news: right now, ember-data doesn't do anything when it gets a 404 on find(). At all. The model sits in the 'loading' state forever.

在我看来,没有非完全愚蠢的选项。我最后可能会做的是在我的DS.Model上添加一个 notFound 属性,而不是返回404,使用 notFound 设置为 true 。这很痛苦,我知道...

There are no non-completely-stupid options, here, in my opinion. What I would probably do as a last resort is add a notFound attribute on my DS.Model, and instead of returning 404, return JSON with notFound set to true. It's painful, I know...

---我最初提供了一个解决方案,覆盖 find 子类 RESTAdapter 。然后我注意到 find 不会传递它被假定加载的记录实例。所以,通过将记录置于错误状态,不要处理404s。

--- I had originally offered a solution of overriding find in a subclass of RESTAdapter. Then I noticed that find DOES NOT get passed the record instance it is supposedly loading. So, no go on handling 404s by putting the record into an error state.

[注意:自2013年3月以来,ember数据发生了巨大变化,此答案中的信息可能会不再有效]

[NOTE: ember-data has changed dramatically since March 2013, the information in this answer may no longer be operative]

这篇关于如何处理路由中的垃圾数据404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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