jeudi 4 juin 2015

Sideloading hasMany relationship causes infinite loop

Since adding the contacts relationship to our appuser model, a GET on the appuser now triggers an infinite loop:

appuser model

name: DS.attr('string'),
contacts: DS.hasMany('contact', {async: false}),
transactions: DS.hasMany('transaction', {async: true}),
open_requests: DS.hasMany('openrequest', {async: false}),

contacts model

name: DS.attr('string'),
appusers: DS.hasMany('appuser', {async: true}),
telephones: DS.hasMany('telephone', {async: false})

appuser serializer

export default DS.RESTSerializer.extend( {
    typeForRoot: function(root) {
        if (root === 'open_requests') return this._super('openrequest');
        return this._super(root);
    },
});

contact serializer

export default DS.RESTSerializer.extend({
    extractCreateRecord: function(store, type, payload, id, requestType) {
        delete payload.contact.telephones;
        return this._super(store, type, payload, id, requestType);
    },
    extractUpdateRecord: function(store, type, payload, id, requestType) {
        delete payload.contact.telephones;
        return this._super(store, type, payload, id, requestType);
    },
});

An image of the stack trace is attached. Unlike most infinite loops this does not trigger an immediate stack overflow - it generates an a few errors per second. It seems some observer is firing, but I have triple checked an nowhere is contact observed on appuser. enter image description here

Even, more strange, this loop occurs even if the contact relationship is an empty array on the appuser response. I read about some old issues regarding serializers calling each other, but not sure if this is the case here. Note that the code works perfectly if the contact relationship is removed or not sent in the json response. Please help!




Aucun commentaire:

Enregistrer un commentaire