webduvet
webduvet

Reputation: 4292

How to limit the number of objects passed into $firebase object?

I'm trying to set the angularjs app with firebase connector angularfire. In my database I have an endless list and I want to display only the last 100 items and any new coming item.

according documentation the function $firebase() takes the the Firebase reference as argument with whatever url it was set up with. It automatically loads the content wich can be assigned to $scope. However what happens if there is a very long list of data? is there any option to limit the number of items pulled from firebase the same way as you would do it with when pulling the data directly from Firebase reference with .limit() ? I didn't find anything in angularfire documentation.

Upvotes: 0

Views: 1715

Answers (1)

Alexander Burakevych
Alexander Burakevych

Reputation: 2426

You can use limit() optionally with startAt() and endAt():

var ref = new Firebase(URL);
var limited = $firebase(ref.limit(100)); // last 100 messages
or 
var limited = $firebase(ref.endAt().limit(100)); // last 100 messages
or 
var limited = $firebase(ref.startAt().limit(100)); // first 100 messages

you can even pass a position from where to startAt or endAt as an argument: https://www.firebase.com/docs/web/api/query/limit.html

Upvotes: 1

Related Questions