2010-02-24 87 views
0

我目前使用Valums JQuery文件上传插件。该插件使用起来非常方便,但我不喜欢代码的外观。因为它占据内部document.ready如:JQuery Ajax插件文件上传简化代码

$(document).ready(function() { 
    var button = $('#button1'), interval; 

    new AjaxUpload(button, { 
     action: 'http://test.com/user/uploadfile', 
     name: 'myfile', 
     onSubmit : function(file, ext){ 
      // change button text, when user selects file   
      button.text('Uploading'); 

      // If you want to allow uploading only 1 file at time, 
      // you can disable upload button 
      this.disable(); 

      // Uploding -> Uploading. -> Uploading... 
      interval = window.setInterval(function(){ 
       var text = button.text(); 
       if (text.length < 13){ 
        button.text(text + '.');      
       } else { 
        button.text('Uploading');    
       } 
      }, 200); 
     }, 
     onComplete: function(file, response){ 
      button.text('Upload Finished'); 

      window.clearInterval(interval); 

      // enable upload button 
      //this.enable(); 

      alert(response);    
     } 
    }); 
}); 

我的问题是可以更简化的代码?所以,我可以有更多或者更少的样子:提前

$(document).ready(function() { 
    var button = $('#button1'), interval; 

    new AjaxUpload(button, {#leads to another function}#); 
}); 

感谢

回答

0

你是新来的Javascript?我记得我第一次使用Javascript & jQuery的经验很有趣,因为我不喜欢我的代码首先查看的方式。问题是,你必须要习惯它,并明白这一切都是有原因的。编写一个自定义插件来做这样的事情会有用,但最终,你仍然会将代码隐藏在某个地方。只需选择您喜欢的代码嵌套方案,并为您的工作感到自豪,因为99%的客人不会真正关心=)