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

Update queryParams with page and perPage #95

Closed
Dragsbaek opened this issue Oct 7, 2015 · 4 comments
Closed

Update queryParams with page and perPage #95

Dragsbaek opened this issue Oct 7, 2015 · 4 comments

Comments

@Dragsbaek
Copy link

In my app, i have an infinity-list of items and a detail-view. I would like to store the current_page, so that navigating back from the detailview will take the user to the last position.

Is that possible?

@hhff
Copy link
Collaborator

hhff commented Oct 8, 2015

We don't currently have first class support for that that - it's a little tricky.

We do track the currentPage on the route though:
https://github.com/hhff/ember-infinity/blob/master/addon/mixins/route.js#L31

However, the lifecycle hooks are probably what you want:

export default Route.extend(InfinityRoute, {
  model() {
    return this.infinityModel('post');
  },
  /* Hook for when the model was updated */
  infinityModelUpdated(lastPageLoaded, totalPages, newObjects) {
    this.set('controller.infintyModelCurrentPage', lastPageLoaded);
  },
  infinityModelLoaded(totalPages) {
    this.set('controller.infintyModelCurrentPage', totalPages);
  }
});

Now your controller (singleton) will always know the last page that the infinityModel loaded.

Now when your route is activated, you can re-initialize your infinity model, and trigger the infinityLoad action until your Route#currentPage === Controller#infinityModelCurrentPage

Let me know if this works out for you @Dragsbaek !

@hhff hhff closed this as completed Nov 21, 2015
@michaeldeitcher
Copy link

I have the same problem and have tried your solution.

I'm not sure how to keep triggering infinityLoad to bring it to the current page.

I tried using scheduling afterrender but that didn't work.

I would also like to keep the scroll position. Please help.

@hhff
Copy link
Collaborator

hhff commented Feb 5, 2016

This @michaeldeitcher !

You'd prob want to do something like this:

export default Route.extend(InfinityRoute, {
  model() {
    let defaultPerPage = 12;
    let infinityPageToRestore = this.get('controller.infinityModelCurrentPage');

    // We have a page to restore from the controller
    if (infinityPageToRestore) {
      // Trigger the initial page load as a single requset
      return this.infinityModel('post', { perPage: defaultPerPage * infinityPageToRestore }).then(() => {
        // reset the _perPage var so that future loads don't use the initial perPage length
        this.set('_perPage', defaultPerPage); 
      });
    }
    return this.infinityModel('post', { perPage: defaultPerPage });
  },

  /* Track the last current page */
  infinityModelUpdated(lastPageLoaded, totalPages, newObjects) {
    this.set('controller.infintyModelCurrentPage', lastPageLoaded);
  },
  infinityModelLoaded(totalPages) {
    this.set('controller.infintyModelCurrentPage', totalPages);
  }
});

@hhff
Copy link
Collaborator

hhff commented Feb 5, 2016

Keeping track of the scroll position is something you'd need to do with a scroll handler in the view & controller

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