ember-localstorage-adapter icon indicating copy to clipboard operation
ember-localstorage-adapter copied to clipboard

Still a possible issue with serializeHasMany, hasMany relation not persisting after reload

Open hussfelt opened this issue 10 years ago • 6 comments

I had an issue today with relations not persisting over a reload using the localStorageAdapter.

Loading fixtures from an action:

Driver.IndexController = Ember.ObjectController.extend({
    actions: {
        /**
         * An action to load fixtures, delete old ones from localStorage
         * @return {[type]}
         */
        loadFixtures: function() {
            this.store.push('Delivery', Driver.Delivery.FIXTURES[0]).save();
            this.store.push('Location', Driver.Location.FIXTURES[0]).save();
            this.store.push('Location', Driver.Location.FIXTURES[1]).save();
            this.store.push('Location', Driver.Location.FIXTURES[2]).save();
            this.store.push('Location', Driver.Location.FIXTURES[3]).save();
            this.store.push('Location', Driver.Location.FIXTURES[4]).save();
        },
    }
});

Models and fixtures


Driver.Location = DS.Model.extend({
    address:    DS.attr('string'),
    delivery:   DS.belongsTo('delivery', {async: true}),
});

Driver.Location.FIXTURES = [
    { id: 1, address: 'Lindvägen 3B', delivery: 1},
    { id: 2, address: 'Länsmansvägen 115', delivery: 1},
    { id: 3, address: 'Lindvägen 25', delivery: 1},
    { id: 4, address: 'Länsmansvägen 209', delivery: 1},
    { id: 5, address: 'Fjärdingsmansvägen 248', delivery: 1}
];


Driver.Delivery = DS.Model.extend({
    company:    DS.attr('string'),
    locations:  DS.hasMany('location', {async: true}),
});

Driver.Delivery.FIXTURES = [
    {
        id: 1,
        company: 'Delivery and Logisticts Ltd',
        locations: [1,2,3,4,5],
    }
];

Temporary fix:

DS.JSONSerializer.reopen({
    serializeHasMany : function(record, json, relationship) {
        var key = relationship.key;

        var relationshipType = record.constructor.determineRelationshipType(
                record.constructor, relationship);

        if (relationshipType === 'manyToNone'
                || relationshipType === 'manyToMany'
                || relationshipType === 'manyToOne') {
            json[key] = Ember.get(record, key).mapBy('id');
            // TODO support for polymorphic manyToNone and manyToMany
            // relationships
        }
    }
});

hussfelt avatar Oct 20 '14 07:10 hussfelt

I'm having the same issue on ember-data-beta.11.

carter avatar Oct 27 '14 14:10 carter

Despite the instructions to not use async: true on associations, that is the only thing that makes them load correctly for me.

mattmcginnis avatar Nov 11 '14 23:11 mattmcginnis

I'm having this issue with 1.0.0-beta.14.1 as well (and perhaps earlier version). You can use async: true, but that makes it difficult to do some operations in your controllers.

justinph avatar Jan 15 '15 19:01 justinph

DEBUG: Ember : 1.10.0 ember-1.10.0.debug.js:3975 DEBUG: Ember Data : 1.0.0-beta.15 ember-1.10.0.debug.js:3975 DEBUG: jQuery : 1.10.2

I tried the temporary fix with and with out async: true on both the hasMany and belongsTo attributes for the related models. And, the child model was not persisted after a reload. Is there an a way get around this?

spsaul20 avatar Mar 10 '15 01:03 spsaul20

I think someone should step up and do a PR.

kurko avatar Mar 20 '15 00:03 kurko

My apologies. I forgot to add the following: App.ApplicationSerializer = DS.LSSerializer.extend()

Once it was added, the temporary fix worked.

spsaul20 avatar Mar 20 '15 03:03 spsaul20