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

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

问题描述

我正在努力理解将在以下场景中使用的工作流程:

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.

但是,当我编辑产品时,我遇到了一个严重的问题,上述解决方案不起作用.Product 模型现在处于错误状态,我无法找到任何方法来获取该产品的副本,但该副本也处于不同状态.

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.

获取记录的新副本: 所以我尝试使用与现有记录相同的 ID 的 App.Product.find(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.

有一个丑陋的解决方法 - 我可以在记录的 statemanager 上调用 transitionTo 并强制它进入我们想要的状态.我没有轻易决定这样做,但在这一点上,我必须继续这个项目,同时等待 ember-data 的发展.因此,如果该记录到目前为止尚未保存,我们可以通过调用将其从无效或错误状态中拯救出来:

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.这将是默认事务,因为一旦 commit 完成,ember-data 的行为是将模型移动到默认事务中被调用它的原始交易.您始终可以通过调用 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-data 的发展,我可以继续完善它.

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 数据模型做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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