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

Add support for nested REST resources #186

Closed
alexslade opened this issue Apr 5, 2012 · 41 comments
Closed

Add support for nested REST resources #186

alexslade opened this issue Apr 5, 2012 · 41 comments
Labels
🏷️ feat This PR introduces a new feature
Milestone

Comments

@alexslade
Copy link

I've seen a couple of discussions about this, and it seems no satisfactory outcome yet?

We've put together a little change to the standard RESTAdapter, that supports custom URLs. I'd like some opinions on it: If you like it, I'll update the tests and RESTAdapter and put in a pull request.

So, you can now define a custom resource URL in your model (I don't think this is the right place, but I couldn't think of a better way).

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  user: DS.belongsTo('App.User'),
  url: (function(){
      return '/users/' + this.getPath('user.id') + '/posts';
    }).property('user')
})

And the RESTAdapter checks for this custom URL.

DS.NestedRESTAdapteri = DS.RESTAdapter.extend({
  createRecord: function(store, type, model) {

    var data = {};
    data[root] = model.toJSON();

    // ---------------- NEW CODE -----------------------
    if(model.get('url')){
      var url = model.get('url');
    } else {
      var root = this.rootForType(type);
      var url = this.buildURL(root);
    }
    // -------------------------------------------------
    this.ajax(url, "POST", {
      data: data,
      success: function(json) {
        this.sideload(store, type, json, root);
        store.didCreateRecord(model, json[root]);
      }
    });
  }
})   

NOTE: We're only using this for our CREATE action, so it'll need some tweaking to work for all actions. I'm happy to do all that if this has a chance of getting merged.

@boy-jer
Copy link

boy-jer commented May 11, 2012

+1 for this but i will suggest you look at how you can make your work an enhancement of pull request #57: which specified a namespace for the REST adaptor which is added to all the ajax URLs. Also see pull request #39, which does something nearer to the enhancement you are proposing but was abandoned in favour of #57 which unfortunately didn't go all the way to address the issue you are trying to fix.

@justinko
Copy link

+1

App.Comment = DS.Model.extend({
  post: DS.belongsTo('App.Post', {nested: true})
});

Note the nested option.

@twalpole
Copy link

I have a commit at twalpole@239ad99 that implements @justinko suggested method of specifying the nested hierarchy and produces the correct nested urls for create, update, delete (along with tests)- It does not yet work for the read since the location of where the fetch comes from is lost going through the store. I'm trying to figure out the best way to handle the read/fetch portion, if anyone has any suggestions I'd be more than happy to implement.

@justinko
Copy link

@twalpole 👍 👏

@boy-jer
Copy link

boy-jer commented May 16, 2012

👏 good progress @twalpole

@twalpole
Copy link

ok - I've added in read support too and submitted it as pull request #257 . Theres probably some refactoring that could be done to clean it up a little bit but I want to get some other eyes on it to see what I missed.

@boy-jer
Copy link

boy-jer commented May 17, 2012

👏 thanks for all your work @twalpole. Pls @justinko and @heeton kindly register your interest or add your votes to the pull request, so that @tomdale and @wycats could see that others want the feature beside me and @twalpole.

@cmeiklejohn
Copy link
Contributor

👍

@ijonas
Copy link

ijonas commented Jun 5, 2012

+1

@wagenet
Copy link
Member

wagenet commented Jun 21, 2012

@boy-jer For what it's worth, "voting" doesn't really hold any value in these discussions.

@boy-jer
Copy link

boy-jer commented Jun 23, 2012

@wagenet, in principle i agree with you but for what it's worth, having large enough people indicate their interest could make you, @tomdale , @wycats and any other key person like @ebryn to add your views and say yes or no in one fell swoop and everyone can move on, so that was the whole essence of the voting call.

@darthdeus
Copy link
Member

+1

@niklas
Copy link

niklas commented Sep 16, 2012

+1

Support to create a hasOne would be awesome, too. For example: with Torrent.hasOne('transfer') the invokation of torrent.createTransfer() should result in a POST to /torrents/23/transfer.

@sebbean
Copy link

sebbean commented Feb 19, 2013

r u the one?

http://www.youtube.com/watch?v=2GrdZQv-5P0

@kraftwer1
Copy link

+1

Where's the bottleneck or the problem? This issue is open since almost a year now, is there any news, @heeton, @twalpole or @wagenet?

Most APIs I've seen are nested, like /articles/3813/comments, it feels a bit limited to fly with /articles/3813?comments=true...

@toranb
Copy link
Contributor

toranb commented Mar 7, 2013

As a workaround, my pull request below adds the ability to create a nested url (using the parent record)

61736c2

So in your example ...

/users/1/posts/

would return all the posts for user with a pk of 1

It would still require a custom adapter as the default RESTAdatper does not support this (the very reason for this pull request as I understand it). But it has worked for me -my django adapter does this today as the default REST backend requires hasMany lookups to use this approach instead of what you get out of the box w/ ember-data

https://github.com/toranb/ember-data-django-rest-adapter

@kraftwer1
Copy link

Nice! I'll have a look at it...

@samstr
Copy link

samstr commented May 5, 2013

Any progress here?

@mindshell
Copy link

Would be great to have nested REST resources. Any progress?

@delijah
Copy link

delijah commented May 23, 2013

+1

@jdjkelly
Copy link
Contributor

+2

@frozeman
Copy link

+3

@dustMason
Copy link

+1

@twalpole
Copy link

twalpole commented Jul 2, 2013

To the people that keep upvoting this, I had put an updated version as PR 790 - but the powers that be decided it was not the approach they wanted to follow.

@kraftwer1
Copy link

Then, IMHO, a clear and official statement somewhere in the guides would be helpful.

@mindshell
Copy link

+1 on the clear & official statement request

@alexslade
Copy link
Author

Sorry for starting and abandoning!
I've not been active with Ember for a few months now, so I'm going to gracefully bow out and let some more informed people argue for me :)

@bugant
Copy link

bugant commented Jul 3, 2013

+1

1 similar comment
@andyhot
Copy link

andyhot commented Jul 9, 2013

+1

@DeathPoison
Copy link

+1

@bugant
Copy link

bugant commented Jul 19, 2013

@sidonath PR looks promising. Is there any chance it will get merged?

@irae
Copy link

irae commented Aug 3, 2013

+1

2 similar comments
@mattsoltani
Copy link

+1

@atonse
Copy link

atonse commented Aug 9, 2013

+1

@wagenet
Copy link
Member

wagenet commented Aug 9, 2013

@wycats
Copy link
Member

wycats commented Sep 3, 2013

The findHasMany adapter hook can be used to add support for this, but we should have a nicer way to opt into this exact scenario and provide the needed URL.

I'll keep it in mind going forward.

@wycats wycats closed this as completed Sep 3, 2013
@cwiese
Copy link

cwiese commented May 31, 2014

This is not solved yet!?!?!?!? So many reason to stop using Ember - after weeks of development around nested Routes and now EmberData ignores the nesting! I have yet to see how this is better than Backbone or Angular

@kstevens715
Copy link

@cwiese
Copy link

cwiese commented May 31, 2014

That is funny. After reading dozens of blogs and github issues, I am starting to believe that the Nesting is only a client side routing mechanism and the REST API should not used nesting (there could be 100's of possible combinations of nesting for a relational model). So I will change the server.

@atsjj
Copy link

atsjj commented Jun 6, 2014

@cwiese FWIW, I have a large Ember project that uses a nested API that goes several layers deep. This is accomplished in our JSON with each returned parent object containing a links hash, with the related model's typekey as the key and value as a URL.

I haven't had any problems with this structuring and it works well enough for what we're doing. We structure our JSON close to the JSONAPI format with the REST adapter. We are looking at switching to the JSONAPI adapter/serializer for ember-data.

@cwiese
Copy link

cwiese commented Jun 6, 2014

Great info - I did see that solution. Thanks

@runspired runspired added 🏷️ feat This PR introduces a new feature and removed Improvement labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ feat This PR introduces a new feature
Projects
None yet
Development

No branches or pull requests