@@ -30,6 +30,7 @@ var topUserPostsSection = document.getElementById('top-user-posts-list');
30
30
var recentMenuButton = document . getElementById ( 'menu-recent' ) ;
31
31
var myPostsMenuButton = document . getElementById ( 'menu-my-posts' ) ;
32
32
var myTopPostsMenuButton = document . getElementById ( 'menu-my-top-posts' ) ;
33
+ var listeningFirebaseRefs = [ ] ;
33
34
34
35
/**
35
36
* Saves a new post to the Firebase DB.
@@ -151,16 +152,23 @@ function createPostElement(postId, title, text, author, authorId, authorPic) {
151
152
152
153
// Listen for likes counts.
153
154
// [START post_value_event_listener]
154
- firebase . database ( ) . ref ( 'posts/' + postId + '/starCount' ) . on ( 'value' , function ( snapshot ) {
155
+ var starCountRef = firebase . database ( ) . ref ( 'posts/' + postId + '/starCount' ) ;
156
+ starCountRef . on ( 'value' , function ( snapshot ) {
155
157
updateStarCount ( postElement , snapshot . val ( ) ) ;
156
158
} ) ;
157
159
// [END post_value_event_listener]
158
160
159
161
// Listen for the starred status.
160
- firebase . database ( ) . ref ( 'posts/' + postId + '/stars/' + uid ) . on ( 'value' , function ( snapshot ) {
162
+ var starredStatusRef = firebase . database ( ) . ref ( 'posts/' + postId + '/stars/' + uid )
163
+ starredStatusRef . on ( 'value' , function ( snapshot ) {
161
164
updateStarredByCurrentUser ( postElement , snapshot . val ( ) ) ;
162
165
} ) ;
163
166
167
+ // Keep track of all Firebase reference on which we are listening.
168
+ listeningFirebaseRefs . push ( commentsRef ) ;
169
+ listeningFirebaseRefs . push ( starCountRef ) ;
170
+ listeningFirebaseRefs . push ( starredStatusRef ) ;
171
+
164
172
// Create new comment.
165
173
addCommentForm . onsubmit = function ( e ) {
166
174
e . preventDefault ( ) ;
@@ -267,9 +275,15 @@ function startDatabaseQueries() {
267
275
} ) ;
268
276
} ;
269
277
278
+ // Fetching and displaying all posts of each sections.
270
279
fetchPosts ( topUserPostsRef , topUserPostsSection ) ;
271
280
fetchPosts ( recentPostsRef , recentPostsSection ) ;
272
281
fetchPosts ( userPostsRef , userPostsSection ) ;
282
+
283
+ // Keep track of all Firebase refs we are listening to.
284
+ listeningFirebaseRefs . push ( topUserPostsRef ) ;
285
+ listeningFirebaseRefs . push ( recentPostsRef ) ;
286
+ listeningFirebaseRefs . push ( userPostsRef ) ;
273
287
}
274
288
275
289
/**
@@ -305,7 +319,19 @@ window.addEventListener('load', function() {
305
319
writeUserData ( user . uid , user . displayName , user . email , user . photoURL ) ;
306
320
startDatabaseQueries ( ) ;
307
321
} else {
322
+ // Display the splash page where you can sign-in.
308
323
splashPage . style . display = '' ;
324
+
325
+ // Remove all previously displayed posts.
326
+ topUserPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
327
+ recentPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
328
+ userPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
329
+
330
+ // Stop all currently listening Firebase listeners.
331
+ listeningFirebaseRefs . forEach ( function ( ref ) {
332
+ ref . off ( ) ;
333
+ } ) ;
334
+ listeningFirebaseRefs = [ ] ;
309
335
}
310
336
} ) ;
311
337
0 commit comments