2010-02-24 23 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%的客人不會真正關心=)