Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hussfelt opened this issue Oct 20, 2014 · 6 comments

Comments

@hussfelt
Copy link

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
        }
    }
});
@carter
Copy link

carter commented Oct 27, 2014

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

@mattmcginnis
Copy link

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

@justinph
Copy link

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.

@spsaul20
Copy link

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?

@kurko
Copy link
Collaborator

kurko commented Mar 20, 2015

I think someone should step up and do a PR.

@spsaul20
Copy link

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

Once it was added, the temporary fix worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants