(function( $ ){ $.fn.user = function( options ) { var settings = $.extend( { op :'login', url :'', panel:'', form:'', callback : '', submitbtn :'', loadbar:'' }, options); var methods = { request : function(){ $.ajax({ 'type':"POST", 'url':settings.url, 'data':$(settings.form).serialize(), 'success':settings.callback, 'timeout': 30000, 'error': function(){ methods.Alert('danger','出现错误,请稍后重试'); }, 'beforeSend': function(){ $(settings.submitbtn).addClass('disabled'); $(settings.submitbtn).attr('disabled','disabled'); $(settings.loadbar).show(); }, complete: function(){ $(settings.submitbtn).removeClass('disabled'); $(settings.submitbtn).removeAttr('disabled'); $(settings.loadbar).hide(); } }); }, register : function(){ methods.request(); }, login : function(){ methods.request(); }, Alert : function(type,content){ $(settings.panel).attr('class','alert alert-'+type); $(settings.panel).html(content); } } var callback = { register : function(data){ if (typeof(data)=='object') { if(typeof(data.error)!='undefined') { methods.Alert('danger',data.error); if(typeof(data.place)!='undefined') { this.input = $('#user-form-register input[name='+data.place+']'); this.input.parent('div').addClass('has-error'); this.input.focus(); } return false; } if(typeof(data.msg)!='undefined') { methods.Alert('danger',data.error); return false; } if(typeof(data.success)!='undefined') { $('#user-register-dismiss').click(); window.location.href = window.location.href; } } else{ methods.Alert('danger','服务器掉链子了,通知管理员揍它'); return false; } }, login : function(data){ if (typeof(data)=='object') { if(typeof(data.error)!='undefined') { methods.Alert('danger',data.error); if(typeof(data.place)!='undefined') { this.input = $('#user-form-login input[name='+data.place+']'); this.input.parent('div').addClass('has-error'); this.input.focus(); } return false; } if(typeof(data.success)!='undefined') { $('#user-login-dismiss').click(); window.location.href = window.location.href; } } else{ methods.Alert('danger','服务器掉链子了,通知管理员揍它'); return false; } } } this.each(function() { if(settings.op == 'register') { settings.url = '/account/register'; settings.panel = '#register-alert'; settings.form = '#user-form-register'; settings.callback = callback.register; settings.submitbtn = '#user-register-btn'; settings.loadbar = '#register-ajax-loading'; $('#user-form-register input').each(function(index, element) { $(this).parent('div').removeClass('has-error'); $(this).bind('focusout',function(){ $(this).parent('div').removeClass('has-error'); }); }); methods.register(); } if(settings.op == 'login') { settings.url = '/account/login'; settings.panel = '#login-alert'; settings.form = '#user-form-login'; settings.callback = callback.login; settings.submitbtn = '#user-login-btn'; settings.loadbar = '#login-ajax-loading'; $('#user-form-login input').each(function(index, element) { $(this).parent('div').removeClass('has-error'); $(this).bind('focusout',function(){ $(this).parent('div').removeClass('has-error'); }); }); methods.login(); } }); }; })( jQuery );