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

Using URL templates in serializer links #29

Closed xtagon closed 7 years ago

xtagon commented 7 years ago

Hi,

I'm loving ember-data-url-templates, thanks for making it!

My REST serializers are for a non-standard API, and I find myself needing to override normalize response functions to add links to the data. If I don't add links in the serializer, Ember-Data will not recognize that there is a has many relationship.

Problem is, I don't want to hard-code each relative link by modifying the payload. I would rather just use ember-data-url-templates and have Ember assume there is a relationship if the template exists. Alternatively, I would like some way to expand/render the template myself while I add links (but the former method would be better)

What is the recommended way to accomplish this?

Thanks in advance!

amiel commented 7 years ago

@xtagon I'm glad you are enjoying ember-data-url-templates

Problem is, I don't want to hard-code each relative link by modifying the payload.

I don't have an ideal solution to the problem you are describing. I personally also use the links technique (although I frequently have my server add the links attribute).

I would rather just use ember-data-url-templates and have Ember assume there is a relationship if the template exists.

The problem, for ember-data-url-templates, is that ember-data does not make any calls to the adapter when a relationship has neither links or some ids.

Alternatively, I would like some way to expand/render the template myself while I add links (but the former method would be better)

You can always use UriTemplate directly.


I'm not sure which would work the best, but here are a few ideas on how to handle relationships with ember-data-url-templates:


I'd like to leave this issue open for reference and as a place for discussion.

amiel commented 7 years ago

@rmharrison You might also be interested in this discussion :)

xtagon commented 7 years ago

Have ember-data-url-templates actually add links

This seems pretty error prone. Normally, as you did, this would happen in the serializer, but it seems pretty weird to need to include ember-data-url-templates in your serializer...

Weirder than hard-coding links in the serializer, which is really the only other thing to do when the server doesn't provide links?

amiel commented 7 years ago

Update: I'd like to address this by resolving https://github.com/emberjs/data/issues/2162.

amiel commented 7 years ago

@xtagon Would this still be useful to you?

As part of working on emberjs/data#2162, I'm taking some extra time to document how this works.

At work, we're actually adding links in the serializer, so I thought I would try implementing your suggestion in ember-data-url-templates.

Do you have time to evaluate https://github.com/amiel/ember-data-url-templates/pull/36 ?

It would be great to get a second set of eyes on it since it is such a big change...

xtagon commented 7 years ago

@amiel This would still be useful and I'll be happy to give #36 a try. We're still adding links in the serializer at work as well.