angular-ui / ui-scroll

Unlimited bidirectional scrolling over a limited element buffer for AngularJS applications
http://angular-ui.github.io/ui-scroll/demo/
MIT License
327 stars 107 forks source link

When the "adapter" is loaded/assigned in the scope? #197

Closed MiguelAngel82 closed 6 years ago

MiguelAngel82 commented 6 years ago

Hi all!

I'm using ui-scroll 1.70 in a 1.6 AngularJS app. The app has a table with a dataSource and adapter.

What I want to do is a periodically reload of the adapter, based on time. So in the "onInit" cycle a simple interval is done every 10 seconds, like that (sorry, is in Coffeescript ;) ):

$onInit: () ->
      @$interval (=>
        @$scope.adapter.reload()
      ), 10000

In the end, this works, but I don't know if is a good practice or the best way to do it, because if I do a console.log(@$scope.adapter) in the "onInit" method, without "interval", it's undefined. Therefore, I think that in the interval is defined because of the time (10 seconds) when is fired.

Could it be a solution? Is there another way to do it to ensure that "adapter" is fully loaded?

Thanks in advance!

dhilt commented 6 years ago

@MiguelAngel82 Thanks for the question! Adapter is being initialized during ui-scroll linking process which is not immediate. One of the approach is to use $scope.$watch on the adapter property (more details could be found in the issue https://github.com/angular-ui/ui-scroll/issues/183). Also, you may try to protect adapter.reload() call with something like $scope.adapter && $scope.adapter.reload ? $scope.adapter.reload() : null. Does this help?

MiguelAngel82 commented 6 years ago

Thank you very much @dhilt for your response! It was ver useful ;) I believe that protecting as you suggested it can be enough.