Last active
September 15, 2017 01:22
-
-
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/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | |
}); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works perfectly! Thanks!