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

Ensure that the json written to localstorage is in the format expected by ember-data. #26

Closed
wants to merge 1 commit into from

Conversation

mattburns
Copy link

Entities with a hasMany relationship should contain an array of IDs, not an array of embedded objects.

Without this fix, embedded objects get written to localstorage which will cannot be restored upon page refresh.

I couldn't figure out why the member "this._data" contains embedded objects. Perhaps it's supposed to as a performance enhancement and the real bug is simply that you shouldn't use JSON.stringify. Perhaps you should be using some other serializer (I had a brief search but couldn't find out how).

I struggled to get any help on this but if someone with more ember experience than me (aka some) can chip in and explain what was going on I'd love to hear it.

…pected by ember-data. That is, in a hasMany relationship, the entity should contain an Array of IDs, not and array of embedded objects.
@mattburns
Copy link
Author

This is all wrong! Don't pull it in!

I've realised the serialization problem is because the hasMany relationship does not have the corresponding belongsTo relationship on the other model. This wasn't enabled because of a separate bug. To solve it I simply override the implementation of "serializeHasMany" in JSONSerializer instead:

/**
 * Have to do this to enable arrays of ids to be written in parent models
 * with 'hasMany" relationships
 */
DS.JSONSerializer.reopen({
    serializeHasMany : function(record, json, relationship) {
        var key = relationship.key;

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

        if (relationshipType === 'manyToNone'
                || relationshipType === 'manyToMany'
                || relationshipType === 'manyToOne') {
            json[key] = Ember.get(record, key).mapBy('id');
        }
    }
});

@mattburns mattburns closed this Oct 16, 2013
@soundasleep
Copy link

This is now recognised as a bug, see: emberjs/data#1678

@kurko
Copy link
Collaborator

kurko commented Mar 5, 2014

I believe we can close it. Please reopen if it's still an issue.

@kurko kurko reopened this Mar 5, 2014
@kurko kurko closed this Mar 5, 2014
@kurko
Copy link
Collaborator

kurko commented Mar 5, 2014

Nevermind, thought it was opened.

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

Successfully merging this pull request may close these issues.

3 participants