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

fixes serializeHasMany relationshipType #1678

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/ember-data/lib/serializers/json_serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ DS.JSONSerializer = Ember.Object.extend({

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

if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany') {
if (relationshipType === 'manyToNone' || relationshipType === 'manyToOne' || relationshipType === 'manyToMany') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three are needed, how about:

    var relationshipTypes = ['manyToNone', 'manyToMany', 'manyToOne'];

    if (relationshipTypes.indexOf(relationshipType) > -1) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't know which one is faster. but yours seems cleaner.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,26 @@ test("serializePolymorphicType", function() {
postTYPE: "post"
});
});


test("serializeHasMany", function() {
Post.reopen({
comments: DS.hasMany('comment')
});

post = env.store.createRecord(Post, { title: "Rails is omakase", id: "1"});
comment = env.store.createRecord(Comment, { body: "Omakase is delicious", post: post});
post.get('comments').addObject(comment);

deepEqual(post.get('comments.content'),
[comment]
);

var json = {};

env.serializer.serializeHasMany(post, json, {key: "comments", options: {}});

deepEqual(json, {
comments: [comment.get('id')]
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ test("serializeIntoHash", function() {

deepEqual(json, {
homePlanet: {
name: "Umber"
name: "Umber",
superVillains: []
}
});
});
Expand All @@ -278,7 +279,8 @@ test("serializeIntoHash with decamelized typeKey", function() {

deepEqual(json, {
homePlanet: {
name: "Umber"
name: "Umber",
superVillains: []
}
});
});