-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathlists-of-data.js
72 lines (62 loc) · 2.21 KB
/
lists-of-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// These samples are intended for Web so this import would normally be
// done in HTML however using modules here is more convenient for
// ensuring sample correctness offline.
import firebase from "firebase/app";
import "firebase/database";
function socialPush() {
// [START rtdb_social_push]
// Create a new post reference with an auto-generated id
var postListRef = firebase.database().ref('posts');
var newPostRef = postListRef.push();
newPostRef.set({
// ...
});
// [END rtdb_social_push]
}
function socialListenChildren() {
const postElement = document.querySelector("#post");
const postId = "1234";
function addCommentElement(el, key, text, author) {}
function setCommentValues(el, key, text, author) {};
function deleteComment(el, key) {};
// [START rtdb_social_listen_children]
var commentsRef = firebase.database().ref('post-comments/' + postId);
commentsRef.on('child_added', (data) => {
addCommentElement(postElement, data.key, data.val().text, data.val().author);
});
commentsRef.on('child_changed', (data) => {
setCommentValues(postElement, data.key, data.val().text, data.val().author);
});
commentsRef.on('child_removed', (data) => {
deleteComment(postElement, data.key);
});
// [END rtdb_social_listen_children]
}
function socialListenValue() {
const ref = firebase.database().ref('/a/b/c');
// [START rtdb_social_listen_value]
ref.once('value', (snapshot) => {
snapshot.forEach((childSnapshot) => {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
// ...
});
});
// [END rtdb_social_listen_value]
}
function socialMostStarred() {
// [START rtdb_social_most_starred]
var myUserId = firebase.auth().currentUser.uid;
var topUserPostsRef = firebase.database().ref('user-posts/' + myUserId).orderByChild('starCount');
// [END rtdb_social_most_starred]
}
function socialMostViewed() {
// [START rtdb_social_most_viewed]
var mostViewedPosts = firebase.database().ref('posts').orderByChild('metrics/views');
// [END rtdb_social_most_viewed]
}
function socialRecent() {
// [START rtdb_social_recent]
var recentPostsRef = firebase.database().ref('posts').limitToLast(100);
// [END rtdb_social_recent]
}