N
N
nepster-web2014-05-25 23:51:58
Yii
nepster-web, 2014-05-25 23:51:58

How to correctly implement sending data to the server not from a form in Yii2?

I am making a tree-like comments module and there is a need for a rating.
The comment template itself looks something like this:

<li class="<?= $class ?>" id="comment-<?= $model['comm_id'] ?>" data-parent="<?= $model['parent_comm_id'] ?>">
   ...
    <div class="vote">
        <a class="upvote">&nbsp;</a>
        <a class="downvote">&nbsp;</a>
    </div>
      ...
</li>

Sending data to the server goes like this:
var csrf_param = $('meta[name=csrf-param]').attr("content");
       var csrf_token = $('meta[name=csrf-token]').attr("content");

        var commentId = $(this).closest('.comment').attr('id').split('-');
       commentId = commentId[1];

$widget.on('click.comments', '.upvote', function() {
            var data = {'_csrf':csrf_token , 'type':'rating', 'vote':'up', 'comm_id':commentId};
                    //console.log(data);
                    requestToServer($(this), '/comments/default/vote', 'POST', data, successVote);
          return false;
        });
                
                
      $widget.on('click.comments', '.downvote', function() {
            var data = {'_csrf':csrf_token , 'type':'rating', 'vote':'down', 'comm_id':commentId};
                    requestToServer($(this), '/comments/default/vote', 'POST', data, successVote);
          return false;
        });

Now the following questions have arisen:
1) where is the best place to write the url address for voting (/comments/default/vote) to throw it in the meta tag or where?
2) maybe if anyone has advice on how to optimize and improve this whole thing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Zelenin, 2014-05-26
@nepster-web

url can be written in the link itself

<a href="/comments/default/vote" class="upvote">&nbsp;</a>

or as a variable:
<script>
var ajax = {
voteUrl: '/comments/default/vote',
...
};
</script>

Since your two methods differ only in the vote variable, it is logical that this should be collected in one method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question