Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active September 15, 2017 01:22
Show Gist options
  • Save katowulf/0e14f81e2a10f75e3fcb to your computer and use it in GitHub Desktop.
Save katowulf/0e14f81e2a10f75e3fcb to your computer and use it in GitHub Desktop.
Simple infinite scroll example in AngularFire with Firebase.util (http://firebase.github.io/firebase-util/)
var app = angular.module('app', ['firebase']);
app.controller('ctrl', function($scope) {
var ref = new Firebase('https://fbutil.firebaseio.com/paginate');
$scope.scrollItems = $scrollArray(ref, 'number');
});
app.factory('$scrollArray', function($firebaseArray) {
return function(ref, field) {
// create a special scroll ref
var scrollRef = new Firebase.util.Scroll(ref, field);
// generate a synchronized array with the ref
var list = $firebaseArray(scrollRef);
// store the scroll namespace on the array for easy ref
list.scroll = scrollRef.scroll;
return list;
}
});
// just some scroll magic to show the bottom of the list as new records are loaded
app.directive('scrollToBottom', function () {
var unbind;
return {
restrict: 'A',
scope: { scrollToBottom: "=" },
link: function (scope, element) {
if( unbind ) { unbind(); }
unbind = scope.$watchCollection('scrollToBottom', function () {
// assumes we have jQuery, which handles the pretty animation
// otherwise, just use this code instead:
// element[0].scrollTop = element[0].scrollHeight;
$(element).animate({scrollTop: element[0].scrollHeight});
});
}
}
});
<body ng-app="app" ng-controller="ctrl">
<h4>Items loaded: {{scrollItems.length}}</h4>
<p>
<button class="btn btn-primary"
ng-click="scrollItems.scroll.next(25)"
ng-disabled="!scrollItems.scroll.hasNext()">
Load Next 25
</button>
</p>
<ul class="scrollbox" scroll-to-bottom="scrollItems">
<li ng-repeat="item in scrollItems">{{item.$id}}: {{item.string}}</li>
</ul>
</body>
@warnerpinz
Copy link

Got Scroll variable undefined

@exeleon
Copy link

exeleon commented May 28, 2016

Works perfectly! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment