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

Computed alias incompatible with relationships #3902

Closed
Asherlc opened this issue Nov 2, 2015 · 6 comments
Closed

Computed alias incompatible with relationships #3902

Asherlc opened this issue Nov 2, 2015 · 6 comments

Comments

@Asherlc
Copy link

Asherlc commented Nov 2, 2015

It appears that Ember.computed.alias doesn’t work for populating relationships:

http://emberjs.jsbin.com/kopive/4/edit?html,css,js,output (with alias, non-working), vs this: http://emberjs.jsbin.com/kopive/2/edit?html,css,js,output (sans alias, working)

@wecc
Copy link
Contributor

wecc commented Nov 3, 2015

If your relationship is named volume instead of book you have to update your payload to reflect the name of the relationship:

relationships: {
  volume: {
    data: { type: 'books', id: '1' }
  }
}

@Asherlc
Copy link
Author

Asherlc commented Nov 3, 2015

Right, but wouldn't the expected behavior of alias be that it would allow for the relationship name change?

@wecc
Copy link
Contributor

wecc commented Nov 3, 2015

Ah, I see what you mean. No, that's not possible :( Relationships are special CPs that ED has to know about beforehand so the alias wouldn't be discovered by ED as a relationship.

@pangratz
Copy link
Member

pangratz commented Nov 3, 2015

If you want to use a different name for a relationship in your payload, you can specify this within your serializer via the attrs hash:

App.AuthorSerializer = DS.JSONAPISerializer.extend({
  attrs: {
    book: { key: 'volume' }
  }
});

Note that there was a bug in JSONAPISerializer when using attrs with relationships. This has been fixed in #3847, available in v2.2.0-beta.2.


If you are using a version of ember-data < v2.2.0-beta.2, you can use keyForRelationship:

App.AuthorSerializer = DS.JSONAPISerializer.extend({
  keyForRelationship: function(key) {
    if ("book" === key) {
      return "volume";
    }

    return this._super(...arguments);
  }
});

@Asherlc
Copy link
Author

Asherlc commented Nov 3, 2015

Ah, that's unfortunate :(. I was hoping it would behave like Rails' alias_attr, which allows exactly that, and is quite useful for me.

@bmac
Copy link
Member

bmac commented Dec 23, 2015

Closing this issue as its working as intended. Ember.computed.alias sets up an alias when working in app code while the serializer is use to configure the payload sent and received by the server.

@bmac bmac closed this as completed Dec 23, 2015
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

4 participants