add like/dislike to answer which is an ObjectId to

2019-04-15 06:42发布

my user schema has an array of answer likes and dislikes on a question by user.

question schema has an ObjectId of answer

in my html, when a user is displayed a list of answers for a particular question, if he likes or dislikes that answer.... the glyphicon should be updated instantly.

the problem occurring is that: due to some reasons the update is taking time and user is able to double click on a glyphicon and increase the like/dislike 2 times.

in console, i see that all answers of all questions are refreshed which makes the processing slow, i guess.

question controller:

    answerlike = false;
    answerdislike = false;
    enter code here
    this.addanswerlike = function(aId){
        answerid = aId;

        answersFactory.addAnswerLike(aId, function(response){
                        $scope.getthatQue(questionid);

        })
    }

    this.removeanswerlike = function(aId){
        asnwerid = aId;

        answersFactory.removeAnswerLike(aId, function(response){
            $scope.getthatQue(questionid);

        })
    }

    this.checkanswerlike = function(ans){
        answersFactory.checkAnswerLike(ans._id, function(response){
            if(response == "liked"){
                ans.likestat = true;  
            }
            else{
                ans.likestat = false;
            }
            console.log('LIKE CHECK chucha :D '+ans)
        })
    }

    /*====================================================================================================================*/

    this.addanswerdislike = function(aId){
        answerid = aId;

        answersFactory.addAnswerDislike(aId, function(response){
            $scope.getthatQue(questionid);
        })
    }

    this.removeanswerdislike = function(aId){
        answerid = aId;

        answersFactory.removeAnswerDislike(aId, function(response){
            $scope.getthatQue(questionid);
        })
    }

    this.checkanswerdislike = function(ans){
        answersFactory.checkAnswerDislike(ans._id, function(response){
            if(response == "disliked"){
                ans.dislikestat = true;  
            }
            else{
                ans.dislikestat = false;
            }
            console.log('DISLIKE CHECK chucha :D '+ans)

        })
    }

    /*====================================================================================================================*/

     $scope.getthatQue = function(que2){
        questionid = que2;
        questionsFactory.getthatQuestion(que2, function(data){
              $scope.que = data;
        })
    }

question schema:

    var QuestionSchema = new Schema({
        title:{type:String, required:true},
        numofanswers:{type:Number, required:true},
        answers: [{type: Schema.Types.ObjectId, ref:'Answer'}]
    })

answer schema :

    var AnswerSchema = new Schema({
        _question: {type: Schema.Types.ObjectId, ref:'Question'},
        answer: {type:String, required:true},
        likes: {type:Number, required:true},
        dislikes: {type: Number, required: true},
    })

user schema:

    var UserSchema = new Schema({
        answer_likes:       [{type: String}],
        answer_dislikes :   [{type:String}]
    });


answer factory:

        factory.addAnswerLike = function(info, callback){
        token = AuthToken.getToken();
        userid = AuthToken.getid();

        $http({
            url:'api/add_like_answer',
            method:'POST',
            headers:{'x-access-token': token},
            params:{'user_id': userid},
            data:{'answer_id':info}
        }).success(function(data){
            console.log('add like: ' + data);
            callback()
        })
    }

    factory.checkAnswerLike = function(info, callback){
        token = AuthToken.getToken();
        userid = AuthToken.getid();

        $http({
            url:'api/check_like_answer',
            method:'POST',
            headers:{'x-access-token': token},
            params:{'user_id': userid},
            data:{'answer_id':info}
        }).success(function(data){
            console.log('check like: ' + data);
            callback(data)
        })
    }

    factory.removeAnswerLike = function(info, callback){
        token = AuthToken.getToken();
        userid = AuthToken.getid();

        $http({
            url:'api/remove_like_answer',
            method:'POST',
            headers:{'x-access-token': token},
            params:{'user_id': userid},
            data:{'answer_id':info}
        }).success(function(data){
            console.log('remove like: ' + data);
            callback()
        })
    }
    factory.addAnswerDislike = function(info, callback){
        token = AuthToken.getToken();
        userid = AuthToken.getid();

        $http({
            url:'api/add_dislike_answer',
            method:'POST',
            headers:{'x-access-token': token},
            params:{'user_id': userid},
            data:{'answer_id':info}
        }).success(function(data){
            console.log('add dislike: ' + data);
            callback()
        })
    }

    factory.checkAnswerDislike = function(info, callback){
        token = AuthToken.getToken();
        userid = AuthToken.getid();

        $http({
            url:'api/check_dislike_answer',
            method:'POST',
            headers:{'x-access-token': token},
            params:{'user_id': userid},
            data:{'answer_id':info}
        }).success(function(data){
            console.log('check dislike: ' + data);
            callback(data)
        })
    }

    factory.removeAnswerDislike = function(info, callback){
        token = AuthToken.getToken();
        userid = AuthToken.getid();

        $http({
            url:'api/remove_dislike_answer',
            method:'POST',
            headers:{'x-access-token': token},
            params:{'user_id': userid},
            data:{'answer_id':info}
        }).success(function(data){
            console.log('remove dislike: ' + data);
            callback()
        })
    }

html:

    <div ng-controller="questionController as questCtrl">
    <div ng-repeat="question in questCtrl.questions | filter: filter_name | orderBy:'-created_at' track by $index">


        <div>
            <button ng-click="getthatQue(question._id)" data-toggle="modal" data-target="#myModal" class="btn btn-default btn-sm">{{question.numofanswers}} Answers </button> 
            <!-- Modal -->

            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">

                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">{{que.title | uppercase}}</h4>
                        </div>
                        <div class="modal-body">
                            <div ng-repeat="ans in que.answers | orderBy:['-likes', '-created_at']">
                                <div>
                                    <div ng-init="questCtrl.checkanswerlike(ans)"> LIKE
                                        <div ng-if="ans.likestat" ng-href="#" ng-click="questCtrl.removeanswerlike(ans._id)" class="glyphicon glyphicon-star "></div>
                                        <div ng-if="!ans.likestat" ng-href="#" ng-click="questCtrl.addanswerlike(ans._id)"  class="glyphicon glyphicon-star-empty "></div> <span ng-bind="ans.likes">
                                    </span> 
                                </div>

                                <div ng-init="questCtrl.checkanswerdislike(ans)"> DISLIKE 
                                    <div ng-if="ans.dislikestat" ng-href="#" ng-click="questCtrl.removeanswerdislike(ans._id)" class="glyphicon glyphicon-star "></div>
                                    <div ng-if="!ans.dislikestat" ng-href="#" ng-click="questCtrl.addanswerdislike(ans._id)"class="glyphicon glyphicon-star-empty "></div> <span ng-bind="ans.dislikes">
                                </span> 
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>

        </div>
    </div>
</div>
</div>
</div>

0条回答
登录 后发表回答