Upvote / Downvote结构问题

时间:2016-06-02 02:28:32

标签: javascript database-design firebase angularfire firebase-realtime-database

我正在制作论坛,就像这样。现在我正在upvote downvote部分......所以首先是我的数据库结构

Topics--
        |__ TOPIC PushKey(Unique Identifier)---
                                           |__Votes---|
                                                      |___ USER UID---|
                                                                      |___-1

        |__ Another TOPIC Pushkey...

正如你在上面看到的,我有我的话题,然后是主题推送键,然后是投票,然后是UserUID,它可以分别识别他,然后分别为-1或1,用于downvote或upvote。现在当我把它放在代码中时:

  $scope.countVote = 0;
        //Viewing Them..
        $scope.votesViewing = $firebaseObject(refService.ref().child("Topics"))
        refService.ref().child("Topics").once("value", function(snapshot) {
            snapshot.forEach(function(childSnapshot) {
                var key = childSnapshot.key();
                var childData = childSnapshot.val();
                if ($stateParams.POST == childData.Postnum) {
                    refService.ref().child("Topics").child(childData.pushKey).child("Vote").on("value", function(snapshotVote) {
                        snapshotVote.forEach(function(VoteChild){
                            VoteChild.forEach(function(evenChildVote){
                                var keyCHILD = evenChildVote.key();
                                var childDataCHILD = evenChildVote.val();
                                $scope.countVote += ( childDataCHILD );
                            })
                        })
                    })
                }
            })
        })


$scope.upVote = function() {
            refService.ref().child("Topics").once("value", function(snapshot) {
                snapshot.forEach(function(childSnapshot) {
                    var key = childSnapshot.key();
                    var childData = childSnapshot.val();
                    if ($stateParams.POST == childData.Postnum) {
                        refService.ref().child("Topics").child(childData.pushKey).child("Vote").child(currentAuth.uid).set({
                            Vote: 1
                        })
                    }
                })
            })

            var upVoteIcon = document.getElementById("upVoteIcon");
            upVoteIcon.classList.remove("thumb-icon");
            upVoteIcon.classList.add("upvote");

            var downVoteIcon = document.getElementById("downVoteIcon");
            downVoteIcon.classList.remove("downvote");
            downVoteIcon.classList.add("thumb-icon");

        }


        $scope.downVote = function() {
            refService.ref().child("Topics").once("value", function(snapshot) {
                snapshot.forEach(function(childSnapshot) {
                    var key = childSnapshot.key();
                    var childData = childSnapshot.val();
                    if ($stateParams.POST == childData.Postnum) {
                        refService.ref().child("Topics").child(childData.pushKey).child("Vote").child(currentAuth.uid).set({
                            Vote: -1
                        })
                    }
                })
            })

            var downVoteIcon = document.getElementById("downVoteIcon");
            downVoteIcon.classList.remove("thumb-icon");
            downVoteIcon.classList.add("downvote");

            var upVoteIcon = document.getElementById("upVoteIcon");
            upVoteIcon.classList.remove("upvote");
            upVoteIcon.classList.add("thumb-icon");

        }

嗯,你可能认为这很顺利但是它有没有......你可以做的是,你可以upvote然后downvote然后upvote ..并且你的upvote计数两次除非你刷新然后你看到正确的数字对于downvote .. downvote upvote downvote为你提供2x downvote你甚至可以从那里更远...我确定这与这个firebase系统有关,它不断渲染新的countVotes,但渲染不正确。有没有办法让它渲染一次然后当我upvote我手动增加$ scope.countVotes一个?如果我尝试$ document.read();或angular.element(准备好),好吧它渲染HTML ...所以它$ scope.countVotes不会出现...还有其他选择吗? (顺便说一句,我没有找力重装......)

演示?你可以查看here,但你必须先注册.. Github? Here它是。但Github没有更新,所以它不会显示正确的代码......

0 个答案:

没有答案