[英]How to get data from Firebase on document ready
I have devloped these firebase script, and I want some help to get like count from firebase data when document ready. 我开发了这些firebase脚本,并且希望获得一些帮助,以便在准备好文档时从firebase数据中获得计数。
Second qousttion,How to make the button (like/dislike) can click once(want the user can only trigger it only once)? 第二个问题,如何使按钮(喜欢/不喜欢)可以单击一次(想要用户仅触发一次)?
Html HTML
// store the main Firebase URL var firebaseURL = 'https://app-api-163321.firebaseio.com/like-button/'; // update the likeCounts shown in a <span> beside each blogpost var postDivs = document.querySelectorAll('.post'); for (var i = 0; i < postDivs.length; i++) { var postID = postDivs[i].id; var numLikes = getLikeCount(postID); } // this function grabs the likeCount for a particular post from the Firebase function getLikeCount(postID) { var thisPostRef = new Firebase(firebaseURL + postID + '/like-count/'); thisPostRef.once('value', function (snapshot) { if (snapshot.val()) { document.querySelector('#' + postID + ' .like-count').innerHTML = snapshot.val() + ' likes'; } else { return 0; } }); } function likePost(id) { var postRef = new Firebase(firebaseURL + id); // get current number of likes here, so we can increment if any exist postRef.child('like-count').once('value', function (snapshot) { var currentLikes = snapshot.val() ? snapshot.val() : 0; postRef.update({ 'postID': id, 'like-count': currentLikes + 1 }, function (error) { console.log(error); }); getLikeCount(id); }); } function dlikePost(id) { console.log('running likePost() for post ID:', id); var postRef = new Firebase(firebaseURL + id); // get current number of likes here, so we can increment if any exist postRef.child('like-count').once('value', function (snapshot) { console.log('snapshot.val():', snapshot.val()); var currentLikes = snapshot.val() ? snapshot.val() : 0; console.log('currentLikes:', currentLikes); postRef.update({ 'postID': id, 'like-count': currentLikes - 1 }, function (error) { if (error) { console.log('Data could not be saved:' + error); } else { console.log('Data saved successfully'); } }); getLikeCount(id); }); }
<script src='https://cdn.firebase.com/js/client/2.2.7/firebase.js'></script> <div class="gpc" id="p72561979729402801623"> <a class="btn btn-success like bl1" data-id="13796" href="#" onclick="likePost('p72561979729402801623');">like</a> <div class="like-count bl2" /> <a class="btn btn-danger dislike bl3" href="#" onclick="dlikePost('p72561979729402801623');">dilike</a> </div>
Are there anyways to do it? 反正有做吗?
https://jsfiddle.net/vj8regwy/ https://jsfiddle.net/vj8regwy/
EDIT (added code for disabling the buttons client-side): You should get the idea and can finish it on your own 编辑(添加了禁用客户端按钮的代码):您应该了解这个想法,并且可以自己完成
<script>
// store the main Firebase URL
var firebaseURL = 'https://app-api-163321.firebaseio.com/like-button/';
function updateLikeCount(postID, count) {
document.querySelector('#' + postID + ' .like-count').innerHTML = count + ' likes';
}
document.addEventListener("DOMContentLoaded", function() {
var postDivs = document.querySelectorAll('.post');
for (var i = 0; i < postDivs.length; i++) {
getLikeCount(postDivs[i].id, updateLikeCount);
}
});
// this function grabs the likeCount for a particular post from the Firebase
function getLikeCount(postID, callback) {
var thisPostRef = new Firebase(firebaseURL + postID + '/like-count/');
return thisPostRef.once('value', function (snapshot) {
if (snapshot.val()) {
callback(postID, snapshot.val());
} else {
callback(postID, 0);
}
});
}
function likePost(postID) {
if (localStorage.getItem('liked') === "true") {
return false;
}
var postRef = new Firebase(firebaseURL + postID);
// get current number of likes here, so we can increment if any exist
postRef.child('like-count').once('value', function (snapshot) {
var currentLikes = snapshot.val() ? snapshot.val() : 0;
postRef.update({
'postID': postID,
'like-count': currentLikes + 1
}, function (error) {
console.log(error);
});
localStorage.setItem('liked', true);
document.getElementById('btn-like').disabled = true;
getLikeCount(postID, updateLikeCount);
});
}
function dlikePost(postID) {
if (localStorage.getItem('disliked') === "true") {
return false;
}
console.log('running likePost() for post ID:', postID);
var postRef = new Firebase(firebaseURL + postID);
// get current number of likes here, so we can increment if any exist
postRef.child('like-count').once('value', function (snapshot) {
console.log('snapshot.val():', snapshot.val());
var currentLikes = snapshot.val() ? snapshot.val() : 0;
console.log('currentLikes:', currentLikes);
postRef.update({
'postID': postID,
'like-count': currentLikes - 1
}, function (error) {
if (error) {
console.log('Data could not be saved:' + error);
} else {
console.log('Data saved successfully');
}
});
localStorage.setItem('disliked', true);
document.getElementById('btn-dislike').disabled = true;
getLikeCount(postID, updateLikeCount);
});
}
</script>
<script src='https://cdn.firebase.com/js/client/2.2.7/firebase.js'></script>
<div class="gpc post" id="p72561979729402801623">
<a class="btn btn-success like bl1" data-id="13796" id="btn-like" href="#" onclick="likePost('p72561979729402801623');">Like</a>
<span class="like-count bl2"></span>
<a class="btn btn-danger dislike bl3" id="btn-dislike" href="#" onclick="dlikePost('p72561979729402801623');">Dislike</a>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.