簡體   English   中英

如何在准備好的文檔中從Firebase獲取數據

[英]How to get data from Firebase on document ready

我開發了這些firebase腳本,並且希望獲得一些幫助,以便在准備好文檔時從firebase數據中獲得計數。

第二個問題,如何使按鈕(喜歡/不喜歡)可以單擊一次(想要用戶僅觸發一次)?

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> 

反正有做嗎?

https://jsfiddle.net/vj8regwy/

編輯(添加了禁用客戶端按鈕的代碼):您應該了解這個想法,並且可以自己完成

    <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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM