[英]Ember JS, Error while parsing Data in Transformer handeling event `didCommit` in state root.loaded.updated.uncommitted."
I am getting an error in an Ember Transformer trying a parse a date in the serialize function. 我在Ember Transformer中尝试在序列化函数中解析日期时收到错误。
Error message: "Attempted to handle event didCommit
on <(subclass of DS.Model):ember1597:8260357> while in state root.loaded.updated.uncommitted." 错误消息:“在状态root.loaded.updated.uncommitted中尝试处理<(DS.Model的子类)上的事件
didCommit
:ember1597:8260357>。”
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"));
行
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. 我尝试了其他一些事情,比如设置静态值或使用
Ember.set
设置值但没有成功。 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. 正如你所知,ember数据不喜欢在变换中使用
set
。 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. Ember希望转换不会修改传入的
serialized
或deserialized
serialized
值,而是返回新值。
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 ). 发生此错误的原因是因为ember有一个状态机来跟踪与服务器相关的记录状态(请参阅http://emberjs.com/api/data/classes/DS.RootState.html )。 In this case ember is most likely putting the record into the
inFlight
state. 在这种情况下,ember很可能将记录置于
inFlight
状态。 It then calls the transform's serialize function. 然后它调用transform的serialize函数。 When
item.set
is called ember notices a change to the record and inadvertently transitions the record back into the uncommitted
state. 当
item.set
,ember会注意到对记录的更改,并且无意中将记录转换回uncommitted
状态。 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. 之后,服务器响应保存请求烬误假设成绩仍然是在
inFlight
状态信号didCommit
事件,这样的记录可以转移到saved
状态。 Because the uncommitted
state doesn't support the didCommit
event ember throws an error. 因为
uncommitted
状态不支持didCommit
事件,所以ember会抛出错误。
I suspect you could fix this issue by rewriting your serialize function to not call set
and instead to return a new array. 我怀疑你可以通过重写你的序列化函数来解决这个问题,而不是调用
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 [];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.