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

EXPERIMENTAL: Add support for relationship urlTemplates #36

Merged
merged 8 commits into from Sep 4, 2017

Conversation

Projects
None yet
3 participants
@amiel
Copy link
Owner

amiel commented Aug 21, 2017

This allows using a url template to load a relationship even if that relationship does not already have data or links.

For some more background on the details of this, check out my blog post on How Ember Data Loads Async Relationships.

I believe this should resolve #29.

Usage

Using this feature will require making changes to a model, serializer, and adapter.

Let's use an example from the acceptance tests. We have a posts model that hasMany reactions and we want to load the reactions with a url template.

To do this, we need to make changes to the post model, post serializer, and post adapter.

In the post model, we opt-in to using a url template to load the relationship by adding the urlTemplate option to hasMany.

// app/models/post.js
reactions: hasMany('reactions', { urlTemplate: 'reactions' }),

In the post serializer, we need to mix in the UrlTemplatesSerializer.

// app/serializers/post.js
import DS from 'ember-data';

import { UrlTemplatesSerializer } from "ember-data-url-templates";

export default DS.JSONAPISerializer.extend(UrlTemplatesSerializer, {
});

In the post adapter, we can specify the url template.

// app/adapters/post.js
reactionsUrlTemplate: "{+host}{/namespace}/posts/{id}/reactions",

Note that the attribute name for the url template (reactionsUrlTemplate) matches the option provided in the options to hasMany ({ urlTemplate: 'reactions' }).

@amiel amiel force-pushed the feature/support-relationship-urls branch from cd8a0a4 to c4c44fe Aug 23, 2017

@xtagon

This comment has been minimized.

Copy link

xtagon commented Aug 29, 2017

Thanks for opening this PR, I will be testing it out when I get a chance 👍

@xtagon

This comment has been minimized.

Copy link

xtagon commented Sep 3, 2017

@amiel This branch works perfectly in my own app and I was easily able to transition away from adding links to my serializers. Thanks so much!

I referenced in my package.json like this:

    "ember-data-url-templates": "github:amiel/ember-data-url-templates#feature/support-relationship-urls",

Looking forward to a release 👍

amiel added some commits Jul 31, 2017

Support urlTemplate model configuration option
by injecting links and overriding hasMany in the adapter
Fix tests in CI by allowing loading posts by id
It seems that when running the tests through ember-try, there's
something that changes causing the acceptance tests to attempt to load
posts by id. My guess is that it has something to do with
`shouldReloadRecord`, but I'm really not sure why that showed up with
ember-try and not when I run the tests normally.

@amiel amiel force-pushed the feature/support-relationship-urls branch from 2b5c7ea to 17620fc Sep 4, 2017

@amiel

This comment has been minimized.

Copy link
Owner

amiel commented Sep 4, 2017

@xtagon Thank you for testing it out. I'll work on a release today.

@amiel amiel merged commit 731bd2e into master Sep 4, 2017

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@amiel amiel deleted the feature/support-relationship-urls branch Sep 4, 2017

@amiel

This comment has been minimized.

Copy link
Owner

amiel commented Sep 4, 2017

Ok 0.4.0 released

@xtagon

This comment has been minimized.

Copy link

xtagon commented Sep 4, 2017

Thanks again @amiel :-)

@dyoganand

This comment has been minimized.

Copy link

dyoganand commented Sep 11, 2017

@amiel I've a slightly different requirement. If the server returns the links, but I need to append some extra params along with the link returned by the server in order to fetch the association data, can I just modify the model to specify the url template, and update my adapter to add the params, but skip the mix in into serializer or is the mixin necessary to be included in the serializer??

@amiel

This comment has been minimized.

Copy link
Owner

amiel commented Sep 11, 2017

@dyoganand If you want to use this feature, mixing in the serializer is required.

For your use-case, it might make sense to custom update the links in your serializer. My blog post on how this feature works might help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment