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

How to load a hasMany relationship? #159

Closed
andrewhavens opened this issue May 13, 2016 · 4 comments
Closed

How to load a hasMany relationship? #159

andrewhavens opened this issue May 13, 2016 · 4 comments

Comments

@andrewhavens
Copy link

I have a route that loads a primary model and displays one of its hasMany relationships. I want to use ember-infinity to paginate the hasMany relationship. Currently, my template simply calls {{#each model.children as |child|}}. What do I need to modify in order to make this work with ember-infinity?

@hhff
Copy link
Collaborator

hhff commented May 24, 2016

Hi @andrewhavens !

Currently we can't do that out of the box - Ember Infinity has a hard dependency on there being an paginated endpoint for the model you're actually displaying.

There's various ways to get around this, but the path of least resistance is to add/modify an API endpoint for your child model, like so:

host.com/api/child_models?parent_model_id=231&per_page=5&page=1

That way you can use a param (or bound param, if it's dynamic) for parent_model_id and paginate that relationship.

Please reopen if this doesn't solve your issue!

@hhff hhff closed this as completed May 24, 2016
@andrewhavens
Copy link
Author

andrewhavens commented May 24, 2016

I think part of my question was simply how to change from a single model route to a route that returns multiple. I ended up with something like this. Posting it here in case it helps anyone else in the future.

# in coffeescript:

  model: (params) ->
    Ember.RSVP.hash
      parent: @store.findRecord('parent', params.parent_id)
      children: @infinityModel('child', parent_id: params.parent_id, modelPath: 'controller.model.children', perPage: 20, startingPage: 1)

@hhff
Copy link
Collaborator

hhff commented May 24, 2016

perfect!

@yogeshjain999
Copy link

Above solution is not working for me. API request is still going to:
host.com/api/children?per_page=5&page=1&parent_id=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants