mardi 29 mars 2016

Unable to make custom serializer work even when the "extractArrat" returns data in correct format(ED 1.0.0 beta 17)

I have 2 models which are defined as:

//exercise-concept-zone model
import DS from 'ember-data';

export default DS.Model.extend({
    reportDataId: DS.attr('string'),
    label: DS.attr('string'),
    exerciseZoneCoordinates: DS.hasMany('exercise-zone-coordinate', {async: true})
});


//exercise-zone-coordinate
import DS from 'ember-data';

export default DS.Model.extend({
    x: DS.attr('integer'),
    y: DS.attr('integer'),
    width: DS.attr('integer'),
    height: DS.attr('integer'),
    isHidden: DS.attr('boolean'),
    exerciseConceptId: DS.attr('integer'),
    locale: DS.attr('string'),
    exerciseConceptZone: DS.belongsTo('exercise-concept-zone')
});

The response from the api looks like this:

[{
    "_id": {
        "class": "org.bson.types.ObjectId",
        "date": "2016-03-29T01:54:11Z",
        "inc": 590530557,
        "machine": -33397647,
        "new": false,
        "time": 1459216451000,
        "timeSecond": 1459216451,
        "timestamp": 1459216451
    },
    "reportDataId": "56f6988ffe025821894bd577",
    "label": "A",
    "coordinates": [{
        "isHidden": false,
        "id": "56f9e043fe0264712332c7fe",
        "overlaps": [],
        "height": 90,
        "locale": "en_US",
        "exerciseConceptId": 2,
        "width": 122,
        "y": 11,
        "x": 462
    }, {
        "isHidden": false,
        "id": "56f9e043fe0264712332c7ff",
        "overlaps": [],
        "height": 90,
        "locale": "en_US",
        "exerciseConceptId": 3,
        "width": 122,
        "y": 11,
        "x": 462
    }],
    "id": "56f9e043fe0264712332c7fd"
}, {
    "_id": {
        "class": "org.bson.types.ObjectId",
        "date": "2016-03-29T02:03:21Z",
        "inc": -1254788286,
        "machine": -33422994,
        "new": false,
        "time": 1459217001000,
        "timeSecond": 1459217001,
        "timestamp": 1459217001
    },
    "reportDataId": "56f6988ffe025821894bd577",
    "label": "B",
    "coordinates": [{
        "id": "56f9e269fe02016eb5357343",
        "isHidden": false,
        "overlaps": [],
        "height": 94,
        "width": 148,
        "exerciseConceptId": 2,
        "locale": "en_US",
        "y": 186,
        "isInvalid": false,
        "x": 279
    }, {
        "id": "56f9e269fe02016eb5357344",
        "isHidden": false,
        "overlaps": [],
        "height": 94,
        "width": 148,
        "exerciseConceptId": 3,
        "locale": "en_US",
        "y": 186,
        "isInvalid": false,
        "x": 279
    }],
    "id": "56f9e269fe02016eb5357342"
}]

And finally my exercise-concept-zone serializer looks like this:

//serializers/exercise-concept-zone
import DS from 'ember-data';

export default DS.JSONSerializer.extend({

    extractArray: function(store, typeClass, arrayPayload, id, requestType) {
        let normalizedPayload = this.normalizePayload(arrayPayload);
        let returnPayload = {exerciseConceptZones: [], exerciseZoneCoordinates: []};

        normalizedPayload.forEach((singlePayload)=> {
            let normalizedSinglePayload = this.normalize(typeClass, singlePayload);
            (returnPayload.exerciseConceptZones).pushObject(normalizedSinglePayload.exerciseConceptZone);
            (returnPayload.exerciseZoneCoordinates).pushObjects(normalizedSinglePayload.exerciseZoneCoordinates);
        });
        return returnPayload;
    },


    normalize: function(typeClass, hash) {

        let zoneCoordinates = [];

        zoneCoordinates.pushObjects((hash.coordinates).map((coordinate)=> {
            delete coordinate._id;
            delete coordinate.overlaps;
            coordinate.exerciseConceptZone = hash.id;
            return coordinate;
        }));
        hash['exerciseZoneCoordinates'] = (hash.coordinates).map((zoneCoordinate)=> {
            return zoneCoordinate.id;
        });

        delete hash.coordinates;
        delete hash._id;

        let returnPayload = {exerciseConceptZone: hash, exerciseZoneCoordinates: zoneCoordinates};

        return this._super.apply(this, [typeClass, returnPayload]);
    }    

});

And then my response serializers' extractArray is properly formatted to return final payload like this:

{
    "exerciseConceptZones": [{
        "reportDataId": "56f6988ffe025821894bd577",
        "label": "A",
        "id": "56f9e043fe0264712332c7fd",
        "exerciseZoneCoordinates": ["56f9e043fe0264712332c7fe", "56f9e043fe0264712332c7ff"]
    }, {
        "reportDataId": "56f6988ffe025821894bd577",
        "label": "B",
        "id": "56f9e269fe02016eb5357342",
        "exerciseZoneCoordinates": ["56f9e269fe02016eb5357343", "56f9e269fe02016eb5357344"]
    }],
    "exerciseZoneCoordinates": [{
        "isHidden": false,
        "id": "56f9e043fe0264712332c7fe",
        "height": 90,
        "locale": "en_US",
        "exerciseConceptId": 2,
        "width": 122,
        "y": 11,
        "x": 462,
        "exerciseConceptZone": "56f9e043fe0264712332c7fd"
    }, {
        "isHidden": false,
        "id": "56f9e043fe0264712332c7ff",
        "height": 90,
        "locale": "en_US",
        "exerciseConceptId": 3,
        "width": 122,
        "y": 11,
        "x": 462,
        "exerciseConceptZone": "56f9e043fe0264712332c7fd"
    }, {
        "id": "56f9e269fe02016eb5357343",
        "isHidden": false,
        "height": 94,
        "width": 148,
        "exerciseConceptId": 2,
        "locale": "en_US",
        "y": 186,
        "isInvalid": false,
        "x": 279,
        "exerciseConceptZone": "56f9e269fe02016eb5357342"
    }, {
        "id": "56f9e269fe02016eb5357344",
        "isHidden": false,
        "height": 94,
        "width": 148,
        "exerciseConceptId": 3,
        "locale": "en_US",
        "y": 186,
        "isInvalid": false,
        "x": 279,
        "exerciseConceptZone": "56f9e269fe02016eb5357342"
    }]
}

My serializers' response does match the json payload format, but still my exercise- zone-model fails to resolve.




Aucun commentaire:

Enregistrer un commentaire