重构Jquery:解析json

发布于 2024-11-02 08:58:34 字数 1747 浏览 1 评论 0原文

我对两个链接使用以下代码:a.vote-down-0 和 a.vote-up-0,它们执行相同的操作,只是每个链接都指定投票是赞成还是反对。

$('a.vote-down-0').click(function() {

    var id = $(this).siblings('.reply-id').val();
    var ajax_auth_token = $('#auth_token').val();
    var c_button = this;


    $.post('user/?action=ajax', {
        vote_type: 'down',
        reply_id: id,
        auth_token: ajax_auth_token
    }, function(data, return_status) {   //return status is just if ajax works or not

        var json_data = jQuery.parseJSON(data);

        switch(json_data.r_message)
        {
           case "success": 
              output = "Yay it works!"; // change 
              $(c_button).removeClass('vote-down-0').addClass('vote-down-1');
              $(c_button).siblings('a.vote-up-0').addClass('vote-up-1').removeClass('vote-up-0'); // ** TODO: this needs to be repeated for all cases below**
           break;

           case "no_vote":
              output = "You've run  out of negative votes.";
           break;

           case "vote_limit":
              output = "You can vote anymore today. Limit is 25 per day.";
           break;

           case "login": 
              output= "You need to login before you can vote.";
           break;

           case "own":
              output = "You cannot vote on your own comment.";
              $(c_button).removeClass('vote-down-0').addClass('vote-down-1');

           break;

           case "already":
              output ="You have already voted on this.";
           break;

           case "session":
              output = "Your login session has expired, please login again.";
           break;

    }

alert(output);

这会读取通过 Json 发回的响应,并针对每种情况给出不同的警报。

有更简单的方法吗?如何重新考虑这一点?

I use the following code for two links: a.vote-down-0 and a.vote-up-0, they do the same thing, except each one specifies whether the vote is up or down.

$('a.vote-down-0').click(function() {

    var id = $(this).siblings('.reply-id').val();
    var ajax_auth_token = $('#auth_token').val();
    var c_button = this;


    $.post('user/?action=ajax', {
        vote_type: 'down',
        reply_id: id,
        auth_token: ajax_auth_token
    }, function(data, return_status) {   //return status is just if ajax works or not

        var json_data = jQuery.parseJSON(data);

        switch(json_data.r_message)
        {
           case "success": 
              output = "Yay it works!"; // change 
              $(c_button).removeClass('vote-down-0').addClass('vote-down-1');
              $(c_button).siblings('a.vote-up-0').addClass('vote-up-1').removeClass('vote-up-0'); // ** TODO: this needs to be repeated for all cases below**
           break;

           case "no_vote":
              output = "You've run  out of negative votes.";
           break;

           case "vote_limit":
              output = "You can vote anymore today. Limit is 25 per day.";
           break;

           case "login": 
              output= "You need to login before you can vote.";
           break;

           case "own":
              output = "You cannot vote on your own comment.";
              $(c_button).removeClass('vote-down-0').addClass('vote-down-1');

           break;

           case "already":
              output ="You have already voted on this.";
           break;

           case "session":
              output = "Your login session has expired, please login again.";
           break;

    }

alert(output);

This reads the response send back via Json and gives a different alert for each case.

Is there an easier way of doing this? How could this be re-factored?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2024-11-09 08:58:34

您可以尝试将 a 标签的类更改为

<a class="vote-up-0"> 

<a class="vote-0 up">

然后可以将这两个函数重构为:

$('a.vote-0').click(function() {
    var voteType =  $(this).is('.up') ? 'up' : 'down';

    ... snip ...

    $.post('user/?action=ajax', {
        vote_type: voteType,
        reply_id: id,
        auth_token: ajax_auth_token
    }
    ...

case success 的代码将变为:

$(c_button).removeClass('vote-0').addClass('vote-1');
$(c_button).siblings('a.vote-0').addClass('vote-1').removeClass('vote-0');

You could try changing the class of your a tags from

<a class="vote-up-0"> 

to

<a class="vote-0 up">

you could then refactor the two functions as:

$('a.vote-0').click(function() {
    var voteType =  $(this).is('.up') ? 'up' : 'down';

    ... snip ...

    $.post('user/?action=ajax', {
        vote_type: voteType,
        reply_id: id,
        auth_token: ajax_auth_token
    }
    ...

The code for case success then simply becomes:

$(c_button).removeClass('vote-0').addClass('vote-1');
$(c_button).siblings('a.vote-0').addClass('vote-1').removeClass('vote-0');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文