简体   繁体   English

模板访问Ember Data hasMany属性时处理拒绝的诺言

[英]Handle rejected promise when Ember Data hasMany property is accessed by template

Is there a standard way of handling errors when a 'findHasMany' call fails? 当'findHasMany'调用失败时,是否存在处理错误的标准方法? Use case: 用例:

Model: App.User
{
DS.hasMany('comments', {'async': true});
}

Template 模板

{{#each comment in comments}}
<p>{{comment.title}}</p>
{{/each}}

The issue is that when the lazy loading of comments fails, due to some server issue for example, I want to be able to respond to that error in the UI (by routing somewhere else, showing a popup about errors on the page, etc). 问题是,当由于某些服务器问题而导致延迟加载注释失败时,我希望能够响应UI中的该错误(通过路由到其他地方,在页面上显示有关错误的弹出窗口等)。 。 At the moment the promise just rejects. 目前,诺言只是拒绝。 I thought that Ember Data might have some hook on the ManyArray for cases like this, but it doesn't seem to, and the store seems to define precisely nothing as the action to carry out in such cases: https://github.com/emberjs/data/blob/v1.0.0-beta.8/packages/ember-data/lib/system/store.js#L1758 - the promise is given a 'resolve' method, but not a reject method. 我以为Ember Data在类似的情况下可能会在ManyArray上挂上一些钩子,但似乎没有,而且在这种情况下,商店似乎并没有明确定义要执行的操作: https : //github.com /emberjs/data/blob/v1.0.0-beta.8/packages/ember-data/lib/system/store.js#L1758-给了promise一个'resolve'方法,但没有拒绝方法。

My options seem to be either subclassing the store, and adding in some reject code there, or subclassing DS.PromiseArray and observing the 'isRejected' property. 我的选择似乎是子类化商店,然后在其中添加一些拒绝代码,或者子类化DS.PromiseArray并观察“ isRejected”属性。 Any thoughts would be very welcome! 任何想法都将受到欢迎!

EDIT: This issue seems to boil down to the fact that, when handling models defined in a route, Ember and Ember Data work well together (you can catch rejecting promises in an error action) there is no similar structure for async requests directly through a template. 编辑:这个问题似乎可以归结为以下事实:处理路由中定义的模型时,Ember和Ember Data可以很好地协同工作(您可以在错误操作中捕获拒绝的Promise),没有直接通过模板。 One solution might be to have an observer in the controller that observes something like 'model.isError', but a failing hasMany relationship does not trigger an error on the owning model. 一种解决方案可能是在控制器中让观察者观察诸如“ model.isError”之类的东西,但是失败的hasMany关系不会触发拥有模型的错误。 I suppose instead I can do 'comments.isRejected', but again, I would have to code that in for every controller that has a model with a hasMany relationship, in other words, all of them, which doesn't seem very satisfactory. 我想我可以做“ comments.isRejected”,但同样,我必须为具有模型具有hasMany关系的每个控制器(换句话说,所有这些)编码,这似乎不太令人满意。 If models had an observable enumerable property (like "hasManyIsError": {comments: false, posts: true}) then it would be easy to observe any of them with 'hasManyIsError.length 如果模型具有可观察的可枚举属性(例如“ hasManyIsError”:{comments:false,posts:true}),那么使用'hasManyIsError.length可以很容易地观察其中的任何一个。

Assuming a var called user that has been fetched, you'd do this: 假设已获取一个名为user的var,您可以这样做:

var itWorked = function(comments) { return comments; }
var itFailed = function(error) { return error; }

user.get("comments").then(itWorked, itFailed);

async: true means it'll get using a promise... so you can use then... you can't do that on a relationship that doesn't specify async: true. async:true表示它将使用诺言...因此您可以使用...然后就不能在未指定async:true的关系上这样做。

[edit] sorry I just realised it might not be obvous that whatever you put in the itFailed function will eval when the request for comments fails, and likewise inversely for itWorked... :) [edit]对不起,我只是意识到,在注释请求失败时,放在itFailed函数中的任何内容可能都不是显而易见的事情,对于itWorked来说,反之亦然... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM