jeudi 8 octobre 2015

Ember-data: How do I set an incoming null value to an empty string value?

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

Aucun commentaire:

Enregistrer un commentaire