Nested URLs for Ember Data's RestAdapter proposal

Is this working yet, if so is there any documentation?

1 Like

I am having a hard fight trying to use this approach: store.find(‘comment’, ‘1/post/15’)

When I pass a string, the " / " is converted to ASC (%2F). I tried ember safeString and escaping manually with “/” but couldn’t solve.

I’ve been working on an addon to making building custom urls easier.

Check out ember-data-url-templates - npm

Please note that it requires ember-data canary (which commits it requires are detailed in the README).

I’d love to get some feedback, so if you’re using it, please let me know, and if you need help getting set up, I’d be happy to pair on it to help get any issues resolved.

:smile:

I think beta.17 should work

@terzicigor Thanks, I’ll update my README and dependencies.

So, lots and lots of discussion but no real conclusion. Will nested routes find their way into Ember Data any time soon. It has always seemed an important omission to me especially since it is very easy to build nested routes with the Ember router and test them with the Fixture Adapter but then it falls apart when implementing a real backend. If you are building against a Rails like RESTful API nested routes has always been the way to go so why does Ember Data seem reluctant to go with it.

2 Likes

+1

What is the current status on this? I have a django backend and we want to have a resource e.g:

List Post comments:

/posts/ID/comments/

Will this ever be easy to implement on the ember side?

Currently this add-on is working for me GitHub - amiel/ember-data-url-templates: an ember-addon to allow building urls with url templates instead of defining buildURL but no idea if this will become the preferred method of nesting url routes.

Lot’s of great proposals. Sad to don’t be able to do the proposal of @tomdale with

store.find('comment', '1/post/15');

Also you can check the PR for RFC: Ember Data url templates : RFC: Ember Data url templates by amiel · Pull Request #4 · emberjs/rfcs · GitHub

But how can we dictate no nesting for a new resource or an index of resources that must be created or listed respectively in the context of a parent? For example:

  1. List comments that belong to a single post. How can we not nest /post/1/comments, or get context without nesting? /comments?post_id=1 is one way, but is that any better?
  2. Creating a new comment on a post. Again we can pass in the post_id as a parameter in comment. But consider how that would impact a controller in something like Rails.
post = Post.find(params[:comment][:post_id])
post.comments.create(params[:comment])

I’m not thinking in Ember here–just in general. Your comment peaked my curiosity.

Any updates with Ember Data 2.0?

ember-data-url-templates does not seem to work well with ember-data 2.0 released here recently.

I’m not sure if it’s been explicitly stated, but another reason I’d like to use nested urls is to put data validation behind the server. By having ember explicitly call IDs from all over the place, one has to trust the client is calling ids correctly, and with authorization. If that’s behind the server logic, it’s much more straightforward to trust that comments loaded from the URL ‘post/1/comments’ are really the comments from post 1 rather than getting a list of IDs from ‘post/1/’ and calling ‘comments/[list-of-ids]’.

It would be fantastic to be able to call: var comments = store.findAll(‘comment’, {post:postID}) or var comment = store.find('comment,id,{post:postID})

jfarlow, what issues are you having with ember-data-url-templates and ember-data 2.0? None of my applications are up to ember-data 2.0, so I haven’t had a chance to try it but I have heard that other people are using ember-data-url-templates with ember-data 2.0. If you’re having any issues, I’d love to hear about them so I can either properly document the issues or fix them :slight_smile:

@amiel please remove your templating form npm, github, because a lot of fools use it, and have same problem, you can not use singelnton man for store. Remove your lib!

@OpakAlex I’m sorry to hear you have such a negative experience with ember-data-url-templates. I would like to understand the issues you speak of to see what can possibly be resolved so it can work for you and others.

Could you expand on what problem you are referring to?

Another approach based on url templates much like discussed above for nested urls is using adapterOptions

this.store.findRecord(
     'comment', 1, 
     { adapterOptions: {context: post_id: 5} }}
);

and then in buildUrl of adapter

buildURL(modelName, id, snapshot, requestType, query) {
   let context = snapshot.adapterOptions.context;
   ... find url template and apply context and id
}