Reputation: 4481
SITUATION:
I currently load the latest posts chronologically with this code:
CODE:
$scope.data = [];
var _n = Math.ceil(($(window).height() - 50) / (350)) + 1;
var _start = <%= lastID %>;
var firstElementsLoaded = false;
$scope.getDataset = function() {
fb.orderByChild('id').startAt(_start).limitToFirst(_n).on("child_added", function(dataSnapshot) {
_start = dataSnapshot.child("id").val() + 1;
console.log("LAST TRUE ID:"+ _start);
$scope.data.push(dataSnapshot.val());
$scope.$apply()
console.log("THE VALUE:"+$scope.data);
firstElementsLoaded = true;
});
};
$scope.getDataset();
QUESTION:
How should I modify it to essentially have the exact same behaviour but to only query posts with scores above 100 according to their chronological order ?
UPDATE:
I have finally found a way to use the "create another index" method with my architecture. But one hurdle remains:
How to copy a childNode from one node to another?
Upvotes: 11
Views: 290
Reputation: 4481
I rethought my architecture and created a specific index for top posts:
if (funPost.score >= global.hotNumber && funPost.hot == false) {
var hotPostRef = firebase.database().ref("hot/section/"+key);
var hotPost = {
title: funPost.title,
image: funPost.image,
id: funPost.id,
key: funPost.key
}
hotPostRef.set(hotPost);
funPostRef.update({"hot": true});
}
else if (funPost.score <= (global.hotNumber - 25) && funPost.hot == true) {
var hotPostRef = firebase.database().ref("hot/section/"+key);
hotPostRef.remove();
funPostRef.update({"hot": false});
}
Upvotes: 3