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

hasMany relationship was broken #1273

Closed

Conversation

amarpalsapure
Copy link

During serializing object, object with hasMany were not getting serialized. Small fix is required. Hope this helps.

@bradleypriest
Copy link
Member

@amarpalsapure could you please add a test

@amarpalsapure
Copy link
Author

I tried to do that, but not sure how to do that in json_serializer.js. I will need to create some temp objects but not sure how to create them. Currently in my project the solution is working. I have posted an answer on stackoverflow also http://stackoverflow.com/a/18739320/1113246

@bradleypriest
Copy link
Member

Hmm, the bug they were running into was actually fixed here be954be

@wycats
Copy link
Member

wycats commented Sep 11, 2013

This definitely needs to be thought through more fully, but the current default serialization strategy is to serialize one-to-many relationships on the belongsTo side.

It doesn't really make sense to serialize it on both sides, but I can see where you might want to serialize it on the many side (for document oriented databases).

You can easily get the behavior you want in your app by overriding the small serializeHasMany method in your own ApplicationSerializer to something like this:

App.ApplicationSerializer = DS.Serializer.extend({
  serializeHasMany: function(record, json, relationship) {
    var key = relationship.key;

    // don't care which kind of hasMany relationship this is
    json[key] = get(record, key).mapBy('id');
  }
});

@bradleypriest maybe we should make this the default behavior and make the "serializes on the belongsTo side" behavior of the AMSerializer? What do you think?

@wycats wycats closed this Sep 11, 2013
@amarpalsapure
Copy link
Author

@bradleypriest I have taken the latest canary build, but the problem still persist.

Thanks @wycats, I did as you suggested and it did work, small change I did was instead of doing get(record, key) I need to do Ember.get(record, key).

@amarpalsapure amarpalsapure deleted the relationship-hasmany branch September 12, 2013 05:35
@wycats
Copy link
Member

wycats commented Sep 12, 2013

In your own app you can just do record.get(key). In Ember itself the coding style is to always use the functional form.

@amarpalsapure
Copy link
Author

Thanks @wycats for the info and it did work.

@bradleypriest
Copy link
Member

@wycats I'm incredibly Rails-biased myself, I've never actually worked with an API that serializes the hasMany.

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.

None yet

3 participants