2010-05-14 71 views
3

目前我有一個文件,我命名爲JQuery.ext.js,其中包括我的所有頁面。在這個文件中我有許多功能是不喜歡的東西下面,包含jQuery ext函數的最佳做法是什麼?

(function($) { 


    /** 
    * Checks to see if a container is empty. Returns true if it is empty 
    * @return bool 
    */ 
    $.fn.isEmptyContainer = function() { 
     //If there are no children inside the element then it is empty 
     return ($(this).children().length <= 0) ? $(this) : false; 
    }; 

    /** 
    * Strip html tags from elements 
    * @return jQuery 
    */ 
    $.fn.stripHTML = function() { 
     var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi; 

     //Loop through all elements that were passed in 
     this.each(function() { 
      $(this).html($(this).html().replace(regexp, "")); 
     }); 
     return $(this); 
    }; 

    /** 
    * This function will check the length of a textarea and not allow the user to go beyond this length. 
    * You should use the onkeypress event to trigger this function 
    * 
    * @param event event This value should always be event when passing it into this function 
    * @param maxlength int The maximum amount of character that the user is allowed to type in a textarea 
    */ 
    $.fn.enforceMaxLength = function(event, maxlength) { 
     //Only allow the client code to use the onkeypress event 
     if(event.type == 'keypress') { 
      //If the client code does not pass in a maxlength then set a default value of 255 
      var charMax = (!maxlength || typeof(maxlength) != "number") ? 255 : maxlength; 

      //If the user is using the backspace character then allow it always 
      if(event.which == 8) { return true; } 
      //Else enforce the length 
      else { return ($(this).val().length <= charMax); } 
     } 

     //Will only get here if the event type is not keypress 
     return false; 
    }; 
})(jQuery); 

是否有另一種方式做到這一點?或者這是大多數人這樣做的方式?

感謝您的幫助,新都

EDITED

添加不同的插件的代碼並刪除了驗證插件

回答

2

這一切都取決於這些功能的範圍。如果你有一個非常通用的你在應用程序中經常使用的函數列表,我會把它們放在一個單獨的文件中,並在必要時包含它們。

但是,這裏有一個小提示:使用適當的術語,實際上是使用插件而不是函數,而且您不符合編寫指南。但我認爲這不是一個主要問題。 :)

此外,您從該文件採樣的功能似乎用作驗證例程,所以我會將它們放在單獨的驗證庫中。更好的是,我會使用類似jquery-validate的東西,並使用$ .validator.addMethod()添加自定義驗證方法。

編輯

至於究竟如何我會組jQuery函數/插件,我會做完全一樣的例子:通過

(function ($) { 
    // my list of functions, plug-ins, or classes; as needed by the application 
}(jQuery); 

但我想它們分組功能。例如:如果您有驗證,格式化和動畫功能/插件,我會創建3個文件:jquery.myvalidation.js,jquery.myformatting.js等。何時使用函數或插件的問題, in,取決於你是否需要訪問「當前」jQuery元素(使用插件),或者不使用(使用函數)。

我在這裏試圖強調的一點是關於以邏輯方式對事物進行分組,以模塊化方式避免不必要的耦合並最大限度地重用。當你編寫一個jQuery插件時,它應該處理一個非常具體的功能(比如自動完成,scrollTo或者驗證插件)

+0

感謝您的幫助favio。實際上,這裏不僅僅是驗證功能。這些恰好是該文件中的前幾個。你說我實際上在這裏使用插件,是的,我已經將它們設置爲插件格式,但是你認爲我應該以不同的方式做到這一點嗎?我將在這裏添加一些其他函數,並刪除驗證函數以向您展示我需要組織的一些其他函數。 – Metropolis 2010-05-17 12:55:48

+0

沒問題。 :)我已經更新了我的答案,希望有幫助! – Favio 2010-05-17 15:05:25

+0

太棒了!非常感謝Favio的幫助。我相信你能幫助我理解,如果我至少在某種程度上是正確的。我會試着更多地思考這個問題,並找到一種在邏輯莊園中區分這些問題的方法。 – Metropolis 2010-05-17 16:09:11

0

退房的official plugin authoring guidelines。另外,Mike Alsup詳細描述了一個非常具體且有用的開發模式here

+0

我已經查看了這兩個......但那些是專門爲插件編寫.....我只是想弄清楚我應該用這些額外的功能做什麼?可以讓他們這樣嗎?還是有更好的方式來組織這個文件? – Metropolis 2010-05-14 18:15:22

相關問題