2010-05-14 70 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

相关问题