当处于错误状态时,您可以使用Ember Data Models做什么? [英] What can you do with Ember Data Models when in the error state?

查看:95
本文介绍了当处于错误状态时,您可以使用Ember Data Models做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解在以下情况下使用的工作流程:

I'm struggling to understand the workflow that would be used in the following scenario:

用户创建一个模型,让我们称之为产品。我们给他们填写一个表单来填写。除了验证之外的某些原因(超时,访问被拒绝等),保存错误在Ember中,这将使模型进入错误状态。从UI的角度来看,我想做的就是在屏幕上放置一个信息(容易),并允许用户再次尝试(显然不是那么容易)。

A user creates a model, let's call it Product. We present them with a form to fill in. The save errors for some reason other than validations (timeout, access denied etc...) In Ember, this puts the model into an error state. From a UI perspective, all I want to do is put a message on the screen (easy) and allow the user to try again (apparently not so easy).

我已经看过它写了很多次,没有重用一个事务。我明白这个逻辑。在新产品的情况下,我简单地创建另一个新产品,合并原始产品(属性,关系)的数据,并用新产品替换我的控制器的内容。这不是很难,但似乎很好地工作,虽然可能(希望)更好的方式。

I've seen it written many times not to reuse a transaction. I understand the logic of that. In the case of a new Product, I simple create another new Product, merge in the data from the original product (attributes, relationships) and replace the content of my controller with the new Product. This wasn't hard and appears to work nicely, although there may be (hopefully) a better way.

然而,当我编辑产品时,我已经运行成为一个严重的问题,上述解决方案不起作用。该产品型号现在处于错误状态,我找不到任何方式来获取此产品的副本,该副本也不在同一状态。

However, when I'm editing a Product, I have run into a serious issue and the above solution does not work. The Product model is now in the error state and I can not find any way to get a copy of this Product that isn't also in the same state.

一旦遇到错误状态,我就不得不知道这个模型能做什么。我尝试了以下内容:

What I cant' figure out is what I can do with this model once it hits the error state. I have tried the following:

回滚:这不行。您无法在错误状态下回滚交易。

Rollback: This doesn't work. You can't rollback a transaction in the error state.

重新加载:与上述相同。不允许在错误状态下重新载入记录。

Reload: Same as above. Not allowed to reload a record in the error state.

获取记录的新副本:所以我尝试App.Product.find( id)与现有记录相同的ID。它只是给我一个现有记录的副本,在错误状态。

Grab a new copy of the record: So I try App.Product.find(id) with the same id as the existing record. It just gives me a copy of the existing record, in the error state.

我希望我在这里缺少一些相当基本的东西。是否有可能将错误状态(或无效的状态)转录出来?

I'm hoping I'm missing something fairly basic here. Is it possible to roll a record nicely out of an error state (or invalid state for that matter)?

如果有一种简单的方式来更改这些模型的状态我们还应该创建一个新的交易来进一步尝试提交吗?

If there is a simple way to change the state of these models, should we still be creating a new transaction for further attempts to commit?

推荐答案

所以经过几天的阅读来源和实验我得出结论,这是尚未实现的功能。要将记录移动到另一个状态,您应该向 statemanager 发送一个事件。似乎没有事件在错误状态上注册,允许我们恢复记录。

So after a few days of reading source and experimenting, I have come to the conclusion that this is functionality that is not yet implemented. To move a record into another state you are supposed to send an event to it which passes it on the statemanager. There appears to be no events registered on the error state that allows us to recover the record.

有一个丑陋的解决方法 - 我可以调用 transitionTo 在记录的 statemanager 中,并强制它进入我们想要的状态。我没有决定轻率地做这件事,但是在这一点上,我必须继续这个项目,而我等待垃圾数据的发展。因此,如果记录到目前为止尚未保存,我们可以通过调用以下方式将其从无效或错误状态中解除:

There is an ugly workaround - I can call transitionTo on the statemanager of the record and force it into the state we want. I did not decide to do this lightly, but at this point I must continue on with the project while I wait for ember-data to evolve. So if the record is so far unsaved, we can rescue it from an invalid or error state by calling:

model.get('stateManager' ).transitionTo('loaded.created.uncommitted')

或现有记录:

model.get('stateManager')。transitionTo('loaded.updated')

一旦这个被调用,你可以尝试在模型所在的事务上再次调用commit。这将是默认事务,因为ember-data的行为是将模型移到默认事务中,一旦调用了commit它是原始交易。您可以随时通过调用 model.get('transaction')来检索模型的当前事务。

Once this has been called, you can attempt to call commit again on the transaction that the model resides in. This will be the default transaction as the behaviour of ember-data is to move a model into the default transaction once commit has been called on it's original transaction. You can always retrieve the current transaction for a model by calling model.get('transaction')

所以在最后,我有一种方法可以创建Ruby on Rails中可能会看到的典型CRUD场景,但我不认为这是理想的方式。但是,我确实相信,在这个时候,ember-data团队也没有。

So at the end of this, I have a way to create the typical CRUD scenario that we might see in Ruby on Rails, but I don't believe this is the ideal way to do it. I do believe however that at this point in time, neither does the ember-data team.

对于那些有兴趣将CRUD功能作为Ember的控制器和路由混合我有一个 Gist ,其中包含我目前使用的代码。这是正常的,恢复从保存错误以及验证错误。希望我可以继续改进,因为ember数据的发展。

For those of you interested in having CRUD functionality as controller and route mixins for Ember, I have a Gist that contains the code I cam currently using. This is working fine, recovers from save errors as well as validation errors. Hopefully I can continue to refine this as ember-data evolves.

这篇关于当处于错误状态时,您可以使用Ember Data Models做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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