如何使用 ember-data 手动将对象状态设置为清理(保存) [英] How to manually set an object state to clean (saved) using ember-data
问题描述
我正在将 ember-data 用于我的一个项目,我有一个问题是关于弄脏对象然后有意将其状态设置为再次清理的可能性 - 而不提交更改.场景是这样的:
I'm using ember-data for a project of mine and I have a question that revolves around the possibility of dirtying an object and then setting its state to clean again on purpose - without commiting the changes. The scenario is this:
假设我通过 banana = App.Fruit.find('banana');
获取了一个对象,并且它有一个黄色水果!"的描述.使用 XHR 长轮询(或 WebSockets),我可能会收到对象的更新版本,因为另一个用户将描述更改为一个美味的黄色水果!"在任何给定的时间点之后我获取了原始对象.
Say I've fetched an object via banana = App.Fruit.find('banana');
and it has a description of "Yellow fruit!". Using XHR long-polling (or WebSockets), I may receive an updated version of the object because of another user having changed the description to "A tasty yellow fruit!" at any given point in time after I fetched the original object.
然后,我想做的是更新对象以反映新收到的数据.为此,我尝试了不同的方法:
Then, what I would like to do is to update the object to reflect the newly received data. For this, I've tried different approaches:
我试过调用
App.Store.load(App.Fruit, new_data);
.首先,这种方法行不通,其次,这不是我真正想要的.我可以自己对对象进行未提交的更改,在这种情况下,仅丢弃这些更改是不可取的(假设load()
调用会覆盖它们).
I've tried calling
App.Store.load(App.Fruit, new_data);
. First of all, this approach doesn't work and secondly, this is not really what I want. I could've made uncommitted changes to the object myself and in this case, it would be undesirable to just discard those (assuming theload()
call would overwrite them).
我尝试遍历新数据,调用 .set()
- 像这样:banana.set('description', new_data.description);代码> - 为了使用新数据更新对象属性(如果适用 = 不脏).这有效,但它使对象处于脏状态.
I've tried looping through the new data, calling .set()
- like so: banana.set('description', new_data.description);
- in order to update the object properties with the new data (where applicable = not dirty). This works but it leaves the object in a dirtied state.
为了再次清理/更新对象 - 并且不让适配器提交更改!- 我查看了物体经过的状态.这些是(至少):
In order to make the object clean/updated again - and not have the adapter commit the changes! - I've taken a look at the states the object travels through. These are (at least):
- 第 1 步:最初,对象处于
rootState.loaded.saved
状态. - 第 2 步:对属性调用
.set()
会将其推送到rootState.loaded.updated.uncommitted
状态. - 第 3 步:调用
App.store.commit();
将对象返回到rootState.loaded.saved
状态.
- Step 1: Initially, the object is in the
rootState.loaded.saved
state. - Step 2: Calling
.set()
on a property pushes it to therootState.loaded.updated.uncommitted
state. - Step 3: Calling
App.store.commit();
returns the object to therootState.loaded.saved
state.
因此,我尝试在第 2 步之后手动将对象状态设置为 saved
,如下所示:banana.get('stateManager').goToState('saved');代码>.
Therefore, I've tried to manually set the object state to saved
after step 2 like so: banana.get('stateManager').goToState('saved');
.
然而,这行不通.下次存储由于任何其他原因提交时,此操作会产生 inFlightDirtyReasons is undefined 错误.
However, this doesn't work. The next time the store commits for any other reason, this maneuver produces an inFlightDirtyReasons is undefined error.
我的问题是:如何手动将脏对象的状态再次更改为干净(已保存)?
My question is: how can I manually change the state of a dirtied object back to clean (saved) again?
推荐答案
查看 ember-data 未提交状态有一个 'becameClean' 事件,因此将记录设置为已加载.已保存.
Looking at ember-data the uncommitted state has a 'becameClean' event which consequently sets the record as loaded.saved.
这应该可以解决问题
record.get('stateManager').send('becameClean');
这篇关于如何使用 ember-data 手动将对象状态设置为清理(保存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!