amiel / ember-data-url-templates

an ember-addon to allow building urls with url templates instead of defining buildURL
https://github.com/amiel/ember-data-url-templates/wiki
MIT License
129 stars 22 forks source link

EXPERIMENTAL: Add support for relationship urlTemplates #36

Closed amiel closed 7 years ago

amiel commented 7 years ago

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' }).

xtagon commented 7 years ago

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

xtagon commented 7 years ago

@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 commented 7 years ago

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

amiel commented 7 years ago

Ok 0.4.0 released

xtagon commented 7 years ago

Thanks again @amiel :-)

dyoganand commented 7 years ago

@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 commented 7 years ago

@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.