Refactored the favorite button ajaxification to a separate function

This commit is contained in:
Craig Andrews 2009-11-11 15:37:17 -05:00
parent c9eb7c1f0b
commit 83b92d9ef0

View File

@ -81,32 +81,6 @@ $(document).ready(function(){
// XXX: refactor this code // XXX: refactor this code
var favoptions = { dataType: 'xml',
beforeSubmit: function(data, target, options) {
$(target).addClass('processing');
return true;
},
success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
var dis = new_form.id;
var fav = dis.replace('disfavor', 'favor');
$('form#'+fav).replaceWith(new_form);
$('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
}
};
var disoptions = { dataType: 'xml',
beforeSubmit: function(data, target, options) {
$(target).addClass('processing');
return true;
},
success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
var fav = new_form.id;
var dis = fav.replace('favor', 'disfavor');
$('form#'+dis).replaceWith(new_form);
$('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
}
};
var joinoptions = { dataType: 'xml', var joinoptions = { dataType: 'xml',
success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true); success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
var leave = new_form.id; var leave = new_form.id;
@ -125,20 +99,8 @@ $(document).ready(function(){
} }
}; };
function addAjaxHidden() {
var ajax = document.createElement('input');
ajax.setAttribute('type', 'hidden');
ajax.setAttribute('name', 'ajax');
ajax.setAttribute('value', 1);
this.appendChild(ajax);
}
$("form.form_favor").ajaxForm(favoptions);
$("form.form_disfavor").ajaxForm(disoptions);
$("form.form_group_join").ajaxForm(joinoptions); $("form.form_group_join").ajaxForm(joinoptions);
$("form.form_group_leave").ajaxForm(leaveoptions); $("form.form_group_leave").ajaxForm(leaveoptions);
$("form.form_favor").each(addAjaxHidden);
$("form.form_disfavor").each(addAjaxHidden);
$("form.form_group_join").each(addAjaxHidden); $("form.form_group_join").each(addAjaxHidden);
$("form.form_group_leave").each(addAjaxHidden); $("form.form_group_leave").each(addAjaxHidden);
@ -252,6 +214,7 @@ $(document).ready(function(){
$('#'+li.id).fadeIn(2500); $('#'+li.id).fadeIn(2500);
NoticeReply(); NoticeReply();
NoticeAttachments(); NoticeAttachments();
NoticeFavors();
} }
} }
$("#notice_data-text").val(""); $("#notice_data-text").val("");
@ -270,8 +233,52 @@ $(document).ready(function(){
NoticeReply(); NoticeReply();
NoticeAttachments(); NoticeAttachments();
NoticeDataAttach(); NoticeDataAttach();
NoticeFavors();
}); });
function addAjaxHidden() {
var ajax = document.createElement('input');
ajax.setAttribute('type', 'hidden');
ajax.setAttribute('name', 'ajax');
ajax.setAttribute('value', 1);
this.appendChild(ajax);
}
function NoticeFavors() {
// XXX: refactor this code
var favoptions = { dataType: 'xml',
beforeSubmit: function(data, target, options) {
$(target).addClass('processing');
return true;
},
success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
var dis = new_form.id;
var fav = dis.replace('disfavor', 'favor');
$('form#'+fav).replaceWith(new_form);
$('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden);
}
};
var disoptions = { dataType: 'xml',
beforeSubmit: function(data, target, options) {
$(target).addClass('processing');
return true;
},
success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true);
var fav = new_form.id;
var dis = fav.replace('favor', 'disfavor');
$('form#'+dis).replaceWith(new_form);
$('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden);
}
};
$("form.form_favor").ajaxForm(favoptions);
$("form.form_disfavor").ajaxForm(disoptions);
$("form.form_favor").each(addAjaxHidden);
$("form.form_disfavor").each(addAjaxHidden);
}
function NoticeReply() { function NoticeReply() {
if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) { if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) {
$('#content .notice').each(function() { $('#content .notice').each(function() {