重构Jquery:解析json
我对两个链接使用以下代码: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将 a 标签的类更改为
,
然后可以将这两个函数重构为:
case success
的代码将变为:You could try changing the class of your a tags from
to
you could then refactor the two functions as:
The code for
case success
then simply becomes: