Ember-data:如何将传入的空值设置为空字符串值? [英] Ember-data: How do I set an incoming null value to an empty string value?
问题描述
我正在使用Ember-Data(v1.13.13)来管理来自服务器的对象。
传入属性的值为null。属性类型是一个默认值为空字符串的字符串。 null不是预期的默认为空字符串。因此,默认情况下,Ember-Data会建立一个可空的字符串数据类型(当然,一般来说,它不是JavaScript的东西)。
无论如何,我想知道如何将传入的null转换为默认的空字符串值,因为模型是实例化的。或者指示Ember-Data根据字符串类型而不是可空字符串类型来考虑属性。
模型(简化):
App.Note = DS.Model.extend({
mystring:DS.attr('string',{defaultValue:''} )
});
传入对象:
{
/ pre>
note:[{
mystring:null
}]
}
内存中的结果值:
<应用。注意:ember1133:1775>
mystring:null
解决方案空和空字符串是不同的,所以并不奇怪,Ember Data的
字符串
转换不会进行转换。但是,您可以编写自己的代码:// transforms / string-null-to-empty.js
export default DS.Transform.extend({
deserialize(serialized){return serialized ||'';},
serialize(deserialized){return deserialized;}
});
然后
mystring:DS.attr('string-null-to-empty')
I am using Ember-Data (v1.13.13) to manage objects coming from the server.
The value of an incoming attribute is null. The attribute type is a string with default value being an empty string. The null is not defaulting to the empty string as expected. So it looks like Ember-Data establishes a nullable string data type by default (thinking about it in general terms, not a JavaScript thing, of course).
In any case, I would like to know how to convert the incoming null to the default empty string value as the model is "instantiated". That or indicate to Ember-Data to consider the property in terms of a string type rather than a nullable string type.
The model (simplified):
App.Note = DS.Model.extend({ mystring: DS.attr('string', { defaultValue: '' }) });
The incoming object:
{ "notes": [{ "mystring": null }] }
The resulting value in memory:
<App.Note:ember1133:1775> mystring: null
解决方案Null and empty string are different, so it is not surprising that Ember Data's
string
transform does not do that conversion. However, you can write your own which does:// transforms/string-null-to-empty.js export default DS.Transform.extend({ deserialize(serialized) { return serialized || ''; }, serialize(deserialized) { return deserialized; } });
then
mystring: DS.attr('string-null-to-empty')
这篇关于Ember-data:如何将传入的空值设置为空字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!