在大型网站上管理document.ready事件 [英] managing document.ready event(s) on a large-scale website

查看:85
本文介绍了在大型网站上管理document.ready事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我现在创建了一个jQuery插件,这是我尝试解决此问题的方法.我确信它会得到改进,并且我可能已经忽略了很多用例,因此,如果有人想随意提供反馈,请:-) https://github.com/WickyNilliams/ReadyBinder

NOTE: I have now created a jQuery plugin which is my attempt of a solution to this issue. I am sure that it could be improved and i've probably overlooked lots of use cases, so if anyone wants to give feedback feel free :-) https://github.com/WickyNilliams/ReadyBinder

我没有这样的问题,但我认为这将是一个有趣的讨论点,我希望人们对此有一些有趣的想法.

I don't have a problem as such, but thought this would be an interesting point for discussion, and i hope people have some interesting ideas for this.

基本上,我在一个大型网站上工作,并且越来越多的我们正在编写越来越多的JavaScript.很好,我喜欢JS的独特方法,并且发现某些较暗的语言版本很古怪;-)然而,一直困扰我的一件事是如何处理文档准备好的事件,因为它们变得越来越大随着时间的流逝(结果是针对的页面不那么集中/特定于所服务的页面)

Basically, i work on a large-scale website and increasingly we are writing more and more JavaScript. This is fine, i enjoy JS' unique approach and i find the quirkiness in some of the darker annals of the language to be endearing ;-) However one thing that has always bugged me is how to manage document ready events as they get increasingly large over time (and as a result less focused/specific to the page being served)

问题是我们只有一个JS文件(合并并缩小了,尽管这对我的问题来说无关紧要).大多数JS是使用显示模块模式编写的,而jQuery是我们选择的框架.因此,我们所有的JS功能都在逻辑上分为方法,命名空间,然后在脚本文件的底部,我们就有了

The problem is that we have one JS file (merged & minified, though that's kind of inconsequential for my questions). Most of the JS is written using the revealing module pattern, and jQuery is our framework of choice. So all our JS funcitonality is logically grouped into methods, namespaced, and then right at bottom of the script file we have this

$(function(){
    //lots of code here, usually calling encapsulated methods 
    //on our namespaced revealing module
});

问题在于此文档就绪处理程序中的并非所有代码都与每个页面相关.例如,在一页上只有10%可能是相关的,而在另一页上也许只有80%是相关的.对我来说,这实在是太不对劲了,我觉得我应该只执行每页我需要的代码,主要是为了提高效率,同时也是为了维护.

The problem is that not all of the code in this document ready handler pertains to every page. For instance, on one page only 10% of it might be relevant, on another perhaps 80% might be relevant. To me, this feels incredibly wrong, i feel i should only execute the code i need per page, mainly for efficiency, but also maintainability.

我已经在Google上搜索了解决此问题的方法,但找不到任何东西,也许我只是在寻找错误的东西!

I've searched google for approaches to this issue, but cannot find anything, maybe i'm just searching for the wrong thing!

无论如何,我的问题是:

Anyway, so my questions are:

  • 有人想到过这个问题吗?
  • 在其他人看来,这真的是一个问题吗?
  • 您的代码中是否有一个大型的,包含所有文档的处理程序,还是更专注于所服务页面的类型?
  • 如果是后者,您将如何处理?在JS中切换了多个处理程序,或者在服务器端动态吐出了准备就绪的处理程序?

期待人们对此事的想法.

Look forward to people's thoughts on the matter.

欢呼

推荐答案

这是我在使用大量JavaScript的rails mvc项目中所做的工作,我为js中的控制器创建了一个单独的命名空间,类似于rails控制器

This is what i have done in my rails mvc project with heavy javascript, i have created a separate namespace for the controllers in js which resembles the rails controller

class BusinessController
   def new
   end  
   def index
   end
end

Var Business =  {
      init : function(action) {
         //code common to the Business module
         //even add the common jquery doc ready here, its clean
         //and call the page specific action
         this[action]();
      },
      new : function() {
             // check jquery document ready code here, thats relevant to this action
             //New rental page specific code here
      },
      index : function() {
             //  check jquery document ready code here, thats relevant to this action
             //index rental page specific code here 
      }
}

在视图代码(服务器端)上,只需通过以下方式启动页面特定的js代码

and on the view code(server side) just initiate the page specific js code by

<script type="text/javascript"> 
 <%= controller_name %>.init('<%= controller.action_name %>'); 
//which will render to
//  Business.init(index);
</script>

您几乎可以对其进行调整,以使其以任何语言运行.而且这种方法并不关心您是一个文件还是多个文件.

You can pretty much tweak this to make it work in any language. And this approach doesn't care whether you have a single file or multiple files.

这篇关于在大型网站上管理document.ready事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆