如何使用ember-data手动设置对象状态进行清理(保存) [英] How to manually set an object state to clean (saved) using ember-data

查看:95
本文介绍了如何使用ember-data手动设置对象状态进行清理(保存)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:



我正在使用ember-data作为我的一个项目,我有一个问题,围绕着弄脏对象的可能性,然后设置它状态再次清理 - 没有提交更改。说明我已经通过 banana = App.Fruit.find('banana'); 它有一个黄色水果!的描述。使用XHR长轮询(或WebSockets),我可能会收到对象的更新版本,因为另一个用户将描述更改为美味的黄色水果!在之后的任何给定时间点,我获取了原始对象。



然后,我想做的是更新对象以反映新接收的数据。为此,我尝试了不同的方法:




  • 我尝试调用 App.Store。加载(App.Fruit,new_data); 。首先,这种方法不行,其次,这不是我想要的。我可以自己对对象做出未提交的更改,在这种情况下,抛出这些错误是不合适的(假设 load()调用将覆盖它们) / p>


  • 我尝试循环使用新数据,调用 .set() - 像这样: code> banana.set('description',new_data.description); - 为了使用新数据更新对象属性(如适用=不脏)。




为了使对象清洁/重新更新 - 并且使适配器提交更改! - 我已经看了对象通过的状态。这些是(至少):




  • 步骤1:最初,对象位于 rootState.loaded.saved 状态。

  • 步骤2:在属性上调用 .set()将其推送到 rootState.loaded.updated.uncommitted state。

  • 步骤3:调用 App.store.commit(); 将对象返回到 rootState.loaded.saved 状态。


$ b $因此,我尝试在步骤2之后手动将对象状态设置为保存,如下所示: banana.get('stateManager' ).goToState('saved');



但是,这不起作用。在下一次由于任何其他原因导致商店出现时,此操作会在FlightDirtyReasons中生成 错误。



问题:



我的问题是:如何手动将脏物体的状态重新改回清洁(保存)?

解决方案

查看ember数据,未提交的状态有一个'gotClean'事件,因此将记录设置为loaded.saved。



  record.get('stateManager')。send('becomeClean'); 


Explanation:

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:

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:

  • 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 the load() call would overwrite them).

  • 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):

  • Step 1: Initially, the object is in the rootState.loaded.saved state.
  • Step 2: Calling .set() on a property pushes it to the rootState.loaded.updated.uncommitted state.
  • Step 3: Calling App.store.commit(); returns the object to the rootState.loaded.saved state.

Therefore, I've tried to manually set the object state to saved after step 2 like so: banana.get('stateManager').goToState('saved');.

However, this doesn't work. The next time the store commits for any other reason, this maneuver produces an inFlightDirtyReasons is undefined error.

Question:

My question is: how can I manually change the state of a dirtied object back to clean (saved) again?

解决方案

Looking at ember-data the uncommitted state has a 'becameClean' event which consequently sets the record as loaded.saved.

This should do the trick

record.get('stateManager').send('becameClean');  

这篇关于如何使用ember-data手动设置对象状态进行清理(保存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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