Reputation: 711
I try to load older messages of my chat messages, like load more or paginate action.
But angularfire doesnt have the limit function, had endAt, startAt, limitToLast and limitToFirst functions, and doesnt work for get only the old messages.
This is my code: the problem with this, is that the load more function refresh the entire list of messages dont append the messages to the list.
var ref = firebase.database().ref("messages/"+userId+"/"+agencyId);
.orderByChild('timestamp')
.limitToLast(2);
scope.messages = $firebaseArray(ref);
scope.loadMore = function(){
ref
.orderByChild('timestamp')
.limitToLast(4);
scope.messages = $firebaseArray(ref);
};
Upvotes: 0
Views: 729
Reputation: 711
So, after a lot of researching, thinking, rethinking. I made my on solution to make a load more messages for a chat message.
var ref = firebase.database().ref();
var messagesRef = ref.child("messages/"+userId+"/"+agencyId);
var query = messagesRef
.orderByChild("timestamp")
.limitToLast(10);
var list = $firebaseArray(query);
scope.blockMessages = [{
list: list
}];
scope.loadMore = function(){
query = messagesRef
.orderByChild("timestamp")
.endAt(list[0].timestamp)
.limitToLast(10);
list = $firebaseArray(query);
list.$loaded().then(function(x){
scope.blockMessages.unshift({
list:list
})
});
};
The loadMore function get the previous 10 records and create a new block of messages. Each block of messages have 10 records, each click on loadMore make a query for the last previous 10 records.
Upvotes: 2