0

I have a standard Master > Details page in AngularJS setup using RouteProvider.

The Master page has a variation of "infinite scrolling" where Data can be added AND removed at both ends. Say, the user is looking at Contacts list, and has scrolled to 312th entry of the Contact List, only Contacts numbered 200-400 are kept, and the entries 1-200 are removed from Scope. Of course, when the user scrolls DOWN beyond 360, the list is appended with 401-500. And if the scrolls UP above 240, the list is "pre-pended" with 100-199.

Now, the user can click on a particular Contact and go to the Details page. Upon pressing 'Back' (window.history.back) how do I make the Master page re-render with,

  1. Exact list 200-400 as it was when the page was left

  2. Scrolled exactly to the position 312

  3. (IMPORTANT) Listeners still listening correctly for scroll events beyond 360 or above 240?

Is this even possible? And if yes, please help me with how.

Nat
  • 280
  • 3
  • 7

1 Answers1

0

I'm not going to write code for this, it's a bit difficult without a deeper understanding of how your infinite scroll works but will offer some ideas

The first thing you would need would be to store the view state in a service and for more robust persistence ( page reloads or return visits) in localStorage synchronized with the service.

The main start point for the view state would be a start index so you know what to filter in your data array ... and either a length value that may or may not be constant in your app or an end index.

As for the scrolling you could use a scroll event handler in a directive to update the service object with offset scrollTop value in order to set on subsequent visit.... angular.element(containerElement)[0].scrollTop = service.scrollTop as a loose example

There is also $anchorScroll service that you could use by setting ID on each item

This won't give you a full solution but hopefully can get you started

Note that you will need to account for time it takes to render ng-repeat by defering to next digest cycle before trying to set scrollTop if you use that

charlietfl
  • 164,229
  • 13
  • 110
  • 143
  • Could you please suggest a mechanism for keeping the listeners alive in the Master page (#3 requirement)? I am surprised that there is not a straight-forward caching solution for my requirements, that we have to write code to manage all of these. Your response validated that I am not crazy to spin my own for this. Thank you for that. – Nat Oct 04 '16 at 19:03