Ember JS,在解析变形金刚处理事件中的数据时出错“didCommit”在状态root.loaded.updated.uncommitted中。 [英] Ember JS, Error while parsing Data in Transformer handeling event `didCommit` in state root.loaded.updated.uncommitted."
问题描述
我在Ember Transformer中遇到一个错误,尝试在serialize函数中解析日期。
错误消息:
尝试处理事件 didCommit
on&((DS.Model ):ember1597:8260357>在状态root.loaded.updated.uncommitted。
奇怪的是,数据传输正确解析到服务器。
代码:
DS.Transform.extend({
deserialize:function(serialized){
var array = [];
if(Ember.isArray(serialized)){
serialized .forEach(function(item){
if(item.feldTyp ===DATE_FIELD&& item.value){
Ember.set(item,value,moment(item。值,DD.MM.YYYY));
}
array.addObject(Ember.Object.create(item));
});
}
return array;
},
serialize:function(deserialized){
if(Ember.isArray(deserialized)){
deserialized.forEach(function(item){
if(item.get('feldTyp')===DATE_FIELD&& item.get('value')){
item.set('value',moment(item.get('value'))。format(DD.MM.YYYY));
}
});
返回反序列化;
}
return [];
}
});
行 item.set('value',moment(item.get ('value'))。format(DD.MM.YYYY));
导致错误被注释掉,错误消失。我尝试过其他的东西,比如设置一个静态值,或者使用 Ember.set
设置值,但是没有成功。我不太清楚这里出了什么问题,所以无法想到解决办法。有人可以帮忙吗?感谢提前。
编辑
解决方法:
我移动序列化成控制器。不看起来优雅,但现在工作...
正如你已经想到,在其变换中使用设置
。因为数据的服务器表示通常不同于在客户端上如何表示数据。 Ember希望转换不要修改 c>序列化的或反序列化的
值,而不是返回新的值。 p>
此错误发生的原因是因为ember具有状态机来跟踪与服务器相关的记录的状态(请参阅 http://emberjs.com/api/data/classes/DS.RootState.html )。在这种情况下,ember最有可能将记录放在Flight 状态下的中。然后调用变换的序列化函数。当
item.set
被称为ember时,会注意到该记录的更改,并无意中将该记录重新转换为未提交的
状态。服务器响应保存请求后,ember错误地假设记录仍然在 inFlight
状态中指示 didCommit
事件该记录可以转换到保存的
状态。因为未提交
状态不支持 didCommit
事件ember会引发错误。
我怀疑您可以通过重写序列化功能来解决这个问题,不要调用 set
,而是返回一个新的数组。
serialize:function(deserialized){
if(Ember.isArray(deserialized)){
return deserialized.map(function(item){
if(item.get('feldTyp')===DATE_FIELD&& item.get('value')){
return moment(item.get('value'))。format(DD.MM.YYYY);
}
});
}
return [];
}
I am getting an error in an Ember Transformer trying a parse a date in the serialize function.
Error message:
"Attempted to handle event didCommit
on <(subclass of DS.Model):ember1597:8260357> while in state root.loaded.updated.uncommitted."
Strangely enough, the data is transmitted correctly parsed to the server.
Code:
DS.Transform.extend({
deserialize : function(serialized) {
var array = [];
if (Ember.isArray(serialized)) {
serialized.forEach(function(item) {
if (item.feldTyp === "DATE_FIELD" && item.value) {
Ember.set(item, "value", moment(item.value, "DD.MM.YYYY"));
}
array.addObject(Ember.Object.create(item));
});
}
return array;
},
serialize : function(deserialized) {
if (Ember.isArray(deserialized)) {
deserialized.forEach(function(item) {
if (item.get('feldTyp') === "DATE_FIELD" && item.get('value')) {
item.set('value', moment(item.get('value')).format("DD.MM.YYYY"));
}
});
return deserialized;
}
return [];
}
});
The line item.set('value', moment(item.get('value')).format("DD.MM.YYYY"));
causes the error as commented out the error vanishes. I tried other things like setting a static value or setting the value using Ember.set
but without success. I do not quite know what went wrong here and thus cannot think of a solution. Can someone help? Thanks in advance.
edit
Workaround: I moved the serialization into the controller. Does not look as elegant but works for now...
As you've figured out ember data doesn't like the use of set
in its transforms. Because the server representation of data is often different from how it is represented on the client side. Ember expects transforms to not modify the serialized
or deserialized
values that are passed in and instead to return new values.
The reason this error is happening is because ember has a state machine to track the state of a record in relation to the server (see http://emberjs.com/api/data/classes/DS.RootState.html). In this case ember is most likely putting the record into the inFlight
state. It then calls the transform's serialize function. When item.set
is called ember notices a change to the record and inadvertently transitions the record back into the uncommitted
state. After the server responds to the save request ember mistakenly assuming the record is still in the inFlight
state signals the didCommit
event so the record can transition to the saved
state. Because the uncommitted
state doesn't support the didCommit
event ember throws an error.
I suspect you could fix this issue by rewriting your serialize function to not call set
and instead to return a new array.
serialize : function(deserialized) {
if (Ember.isArray(deserialized)) {
return deserialized.map(function(item) {
if (item.get('feldTyp') === "DATE_FIELD" && item.get('value')) {
return moment(item.get('value')).format("DD.MM.YYYY");
}
});
}
return [];
}
这篇关于Ember JS,在解析变形金刚处理事件中的数据时出错“didCommit”在状态root.loaded.updated.uncommitted中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!